Skip to content

Commit

Permalink
Method Page has been superseded by RenderView (page* methods have bee…
Browse files Browse the repository at this point in the history
…n deprecated and will be removed soon)
  • Loading branch information
danieleteti committed Dec 22, 2024
1 parent 60a2774 commit 55a4de2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,10 @@ TMyController = class(TMVCController)
procedure Error;
end;

TMyExceptionSeverity = (Fatal, Error, Warning, Information);

EMyException = class(Exception)
private
FSeverity: TMyExceptionSeverity;
FCode: Integer;
FDetails: string;
FDiagnostics: string;
FExpression: string;
public
constructor Create(Msg: string; ASeverity: TMyExceptionSeverity; ACode: Integer; ADetails, ADiagnostics, AExpression: string);
property Severity: TMyExceptionSeverity read FSeverity write FSeverity;
property Code: Integer read FCode write FCode;
property Details: string read FDetails write FDetails;
property Diagnostics: string read FDiagnostics write FDiagnostics;
property Expression: string read FExpression write FExpression;
end;

implementation

uses
MVCFramework.Logger, System.NetEncoding;
MVCFramework.Logger, System.NetEncoding, TemplatePro;

procedure TMyController.GetCustomer(const ID: Integer);
begin
Expand All @@ -57,60 +39,52 @@ procedure TMyController.GetCustomer(const ID: Integer);

procedure TMyController.Index;
begin
raise EMyException.Create('My Custom Error', Fatal, 25, 'some real problem', 'Ensure Patient resource is valid',
'Patient/Identifier/value');
raise EMVCException.Create(500, 'My Custom Error');
end;

procedure TMyController.OnException(const aContext: TWebContext; const aException: Exception; var aHandled: Boolean);
var
lColor: string;
lStatusCode: Word;
begin
inherited;
StatusCode := HTTP_STATUS.InternalServerError;
if Context.Request.ClientPrefer(TMVCMediaType.APPLICATION_JSON) then
begin
aHandled := False;
Exit;
end;

ContentType := TMVCMediaType.TEXT_HTML;
aContext.Response.Content := 'This is an error: ' + aException.Message;
aContext.Response.StatusCode := HTTP_STATUS.InternalServerError;
if aException is EMyException then
aHandled := True;
if aException is EMVCException then
begin
case EMyException(aException).Severity of
Fatal, TMyExceptionSeverity.Error:
lColor := 'red';
Warning:
lColor := 'yellow';
Information:
lColor := 'blue';
else
lColor := 'black';
end;
lStatusCode := EMVCException(aException).HTTPStatusCode;
if lStatusCode >= 500 then
lColor := 'red'
else if lStatusCode >= 400 then
lColor := 'yellow'
else if lStatusCode > 200 then
lColor := 'blue';
aContext.Response.ContentType := TMVCMediaType.TEXT_HTML;
aContext.Response.Content := '<html><body><h1>Error occurred</h1>' + Format('<h2 style="color: %s">', [lColor]) +
TNetEncoding.HTML.Encode(EMyException(aException).ToString) + '</h2>' +
'<p>your truly custom controller exception handler...</p>' + '</body></html>';
aHandled := True;
aContext.Response.Content := TTProCompiler.CompileAndRender(
'<html><body><h1>Error occurred</h1>' +
'<h2 style="color: {{:color}}">{{:text}}</h2>' +
'<p>your truly custom controller exception handler...</p></body></html>', ['color','text'], [lColor, aException.ToString]);
aContext.Response.StatusCode := StatusCode;
end
else if aException is EMVCException then
else
begin
aContext.Response.ContentType := TMVCMediaType.TEXT_HTML;
aContext.Response.Content := '<html><body><h1>Error occurred</h1>' + Format('<h2 style="color: red">', [lColor]) +
TNetEncoding.HTML.Encode(aException.Message) + '</h2>' + '<p>your truly custom controller exception handler...</p>' + '</body></html>';
aHandled := True;
aContext.Response.StatusCode := HTTP_STATUS.InternalServerError;
aContext.Response.Content := TTProCompiler.CompileAndRender(
'<html><body><h1>Generic Error Occurred</h1>' +
'<h2 style="color: red">{{:text}}</h2>' +
'<p>your truly custom controller exception handler...</p></body></html>', ['text'], [aException.ToString]);
end;

end;

procedure TMyController.Error;
begin
raise Exception.Create('Standard Error');
end;

constructor EMyException.Create(Msg: string; ASeverity: TMyExceptionSeverity; ACode: Integer; ADetails, ADiagnostics, AExpression: string);
begin
inherited Create(Msg);
FSeverity := ASeverity;
FCode := ACode;
FDetails := ADetails;
FDiagnostics := ADiagnostics;
FExpression := AExpression;
end;

end.
37 changes: 27 additions & 10 deletions sources/MVCFramework.pas
Original file line number Diff line number Diff line change
Expand Up @@ -933,13 +933,18 @@ TMVCController = class(TMVCRenderer)
/// </summary>
procedure SetPagesCommonFooters(const AViewNames: TArray<string>);

function Page(const AViewNames: TArray<string>; const UseCommonHeadersAndFooters: Boolean = True; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback = nil): string; overload; deprecated 'Use RenderViews';
function Page(const AViewName: string; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback = nil): string; overload; deprecated 'Use RenderView';

/// <summary>
/// Page calls GetRenderedView with sensible defaults.
/// Page method just concatenate -> commonheader_header_views + views + commonfooter_views
/// PageFragment ignore header and footer views
/// RenderTemplate renders all views concatenating them (optionally with commone headers and footers) with sensible defaults.
/// </summary>
function Page(const AViewNames: TArray<string>; const UseCommonHeadersAndFooters: Boolean = True; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback = nil): string; overload; inline;
function Page(const AViewName: string; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback = nil): string; overload;

function RenderViews(const AViewNames: TArray<string>; const UseCommonHeadersAndFooters: Boolean = True; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback = nil): string; overload; inline;
/// <summary>
/// RenderTemplate renders a single view with sensible defaults.
/// </summary>
function RenderView(const AViewName: string; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback = nil): string; overload;

/// <summary>
/// Load mustache view located in TMVCConfigKey.ViewsPath
Expand Down Expand Up @@ -4429,15 +4434,12 @@ procedure TMVCController.OnException(const aContext: TWebContext; const aExcepti

function TMVCController.Page(const AViewName: string; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback): string;
begin
Result := GetRenderedView([AViewName], OnBeforeRenderCallback);
Result := RenderView(AViewName, OnBeforeRenderCallback);
end;

function TMVCController.Page(const AViewNames: TArray<string>; const UseCommonHeadersAndFooters: Boolean; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback): string;
begin
if UseCommonHeadersAndFooters then
Result := GetRenderedView(fPageHeaders + AViewNames + fPageFooters, OnBeforeRenderCallback)
else
Result := GetRenderedView(AViewNames, OnBeforeRenderCallback);
Result := RenderViews(AViewNames, UseCommonHeadersAndFooters, OnBeforeRenderCallback);
end;

procedure TMVCController.PushObjectToView(const aModelName: string; const AModel: TObject);
Expand All @@ -4450,6 +4452,21 @@ procedure TMVCController.RaiseSessionExpired;
raise EMVCSessionExpiredException.Create('Session expired.');
end;

function TMVCController.RenderView(const AViewName: string;
const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback): string;
begin
Result := GetRenderedView([AViewName], OnBeforeRenderCallback);
end;

function TMVCController.RenderViews(const AViewNames: TArray<string>; const UseCommonHeadersAndFooters: Boolean;
const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback): string;
begin
if UseCommonHeadersAndFooters then
Result := GetRenderedView(fPageHeaders + AViewNames + fPageFooters, OnBeforeRenderCallback)
else
Result := GetRenderedView(AViewNames, OnBeforeRenderCallback);
end;

procedure TMVCRenderer.Redirect(const AUrl: string);
begin
GetContext.Response.RawWebResponse.SendRedirect(AUrl);
Expand Down

0 comments on commit 55a4de2

Please sign in to comment.