Skip to content

Commit

Permalink
Added GetErrorPageHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Nov 13, 2024
1 parent dfd80de commit b26a8d6
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 110 deletions.
17 changes: 13 additions & 4 deletions ideexpert/DMVC.Expert.CodeGen.Commands.pas
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,12 @@ procedure TUnitWebModuleDeclarationCommand.ExecuteImplementation(
var
activerecord_con_def_name: string;
activerecord_con_def_filename: string;
default_media_type: string;
begin
inherited;

default_media_type := 'TMVCConstants.DEFAULT_CONTENT_TYPE';

Section
.AppendLine
.AppendLine('implementation')
Expand All @@ -790,23 +794,28 @@ procedure TUnitWebModuleDeclarationCommand.ExecuteImplementation(
.AppendLine(' System.IOUtils,')
.AppendLine(' MVCFramework.Commons,');



if Model.B[TConfigKey.program_ssv_templatepro] then
begin
Section
.AppendLine(' MVCFramework.View.Renderers.TemplatePro,')
.AppendLine(' MVCFramework.View.Renderers.TemplatePro,');
default_media_type := 'TMVCMediaType.TEXT_HTML';
end;

if Model.B[TConfigKey.program_ssv_webstencils] then
begin
Section
.AppendLine(' MVCFramework.View.Renderers.WebStencils,')
.AppendLine(' MVCFramework.View.Renderers.WebStencils,');
default_media_type := 'TMVCMediaType.TEXT_HTML';
end;


if Model.B[TConfigKey.program_ssv_mustache] then
begin
Section
.AppendLine(' MVCFramework.View.Renderers.Mustache,')
.AppendLine(' MVCFramework.View.Renderers.Mustache,');
default_media_type := 'TMVCMediaType.TEXT_HTML';
end;

Section
Expand All @@ -826,7 +835,7 @@ procedure TUnitWebModuleDeclarationCommand.ExecuteImplementation(
.AppendLine(' // session timeout (0 means session cookie)')
.AppendLine(' Config[TMVCConfigKey.SessionTimeout] := dotEnv.Env(''dmvc.session_timeout'', ''0'');')
.AppendLine(' //default content-type')
.AppendLine(' Config[TMVCConfigKey.DefaultContentType] := dotEnv.Env(''dmvc.default.content_type'', TMVCConstants.DEFAULT_CONTENT_TYPE);')
.AppendLine(' Config[TMVCConfigKey.DefaultContentType] := dotEnv.Env(''dmvc.default.content_type'', ' + default_media_type + ');')
.AppendLine(' //default content charset')
.AppendLine(' Config[TMVCConfigKey.DefaultContentCharset] := dotEnv.Env(''dmvc.default.content_charset'', TMVCConstants.DEFAULT_CONTENT_CHARSET);')
.AppendLine(' //unhandled actions are permitted?')
Expand Down
1 change: 0 additions & 1 deletion samples/custom_exception_handling/WebModuleU.dfm
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
object MyWebModule: TMyWebModule
OldCreateOrder = False
OnCreate = WebModuleCreate
OnDestroy = WebModuleDestroy
Actions = <>
Expand Down
1 change: 1 addition & 0 deletions samples/custom_exception_handling/WebModuleU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ procedure TMyWebModule.WebModuleCreate(Sender: TObject);
var
lColor: string;
begin
WebContext.Response.StatusCode := HTTP_STATUS.InternalServerError;
if E is EMyException then
begin
case EMyException(E).Severity of
Expand Down
22 changes: 5 additions & 17 deletions sources/MVCFramework.Commons.pas
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ interface

type

TMVCHTTPMethodType = (httpGET, httpPOST, httpPUT, httpDELETE, httpPATCH, httpHEAD, httpOPTIONS,
httpTRACE);
TMVCHTTPMethodType = (httpGET, httpPOST, httpPUT, httpDELETE, httpPATCH, httpHEAD, httpOPTIONS, httpTRACE);

TMVCHTTPMethods = set of TMVCHTTPMethodType;

Expand Down Expand Up @@ -167,6 +166,7 @@ TMVCConfigKey = record
MaxRequestSize = 'max_request_size'; // bytes
HATEOSPropertyName = 'hateos';
LoadSystemControllers = 'load_system_controllers';
ErrorPageURL = 'error_page_url';
end;

TMVCHostingFrameworkType = (hftUnknown, hftIndy, hftApache, hftISAPI);
Expand Down Expand Up @@ -557,15 +557,6 @@ TMVCStringPairList = class(TObjectList<TMVCStringPair>)
function AddPair(const Key, Value: String): TMVCStringPairList;
end;

// TMVCViewDataSet = class(TObjectDictionary<string, TDataset>)
// private
// { private declarations }
// protected
// { protected declarations }
// public
// constructor Create;
// end;

TMVCCriticalSectionHelper = class helper for TCriticalSection
public
procedure DoWithLock(const AAction: TProc);
Expand Down Expand Up @@ -844,13 +835,15 @@ procedure dotEnvConfigure(const dotEnvDelegate: TFunc<IMVCDotEnv>);
implementation

uses
MVCFramework,
IdCoder3to4,
System.NetEncoding,
System.Character,
MVCFramework.Serializer.JsonDataObjects,
MVCFramework.Utils,
System.RegularExpressions,
MVCFramework.Logger, MVCFramework.Serializer.Commons;
MVCFramework.Logger,
MVCFramework.Serializer.Commons;

var
GlobalAppName, GlobalAppPath, GlobalAppExe: string;
Expand Down Expand Up @@ -944,11 +937,6 @@ function IsReservedOrPrivateIP(const AIP: string): Boolean;
Exit(True);
end;

// function IP2Long(const AIP: string): UInt32;
// begin
// Result := IdGlobal.IPv4ToUInt32(AIP);
// end;

function B64Encode(const aValue: string): string; overload;
begin
// Do not use TNetEncoding
Expand Down
19 changes: 14 additions & 5 deletions sources/MVCFramework.View.Renderers.TemplatePro.pas
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ implementation
MVCFramework.Serializer.Defaults,
MVCFramework.Serializer.Intf,
MVCFramework.DuckTyping,
MVCFramework.Cache,
TemplatePro,
Data.DB,
System.Rtti,
JsonDataObjects;
JsonDataObjects,
TemplatePro;

{$WARNINGS OFF}

Expand Down Expand Up @@ -94,11 +93,21 @@ function DumpAsJSONString(const aValue: TValue; const aParameters: TArray<string
var
lWrappedList: IMVCList;
begin
if not aValue.IsObject then
if aValue.IsEmpty then
begin
Exit('');
end
else if not aValue.IsObject then
begin
if aValue.IsType<Int64> then
begin
Exit(aValue.AsInt64);
end else if aValue.IsType<Integer> then
begin
Exit(aValue.AsInteger);
end else if aValue.IsType<string> then
begin
Exit(aValue.AsString);
end;
Exit('(Error: Cannot serialize non-object as JSON)');
end;
Expand Down Expand Up @@ -178,7 +187,7 @@ procedure TMVCTemplateProViewEngine.Execute(const ViewName: string; const Builde
begin
for lPair in ViewModel do
begin
lCompiledTemplate.SetData(lPair.Key, ViewModel[lPair.Key]);
lCompiledTemplate.SetData(lPair.Key, lPair.Value);
end;
lCompiledTemplate.SetData('LoggedUserName', WebContext.LoggedUser.UserName);
end;
Expand Down
18 changes: 18 additions & 0 deletions sources/MVCFramework.View.Renderers.WebStencils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ TMVCWebStencilsViewEngine = class(TMVCBaseViewEngine)
protected
procedure RegisterWSFunctions(WSProcessor: TWebStencilsProcessor);
procedure OnGetValue(Sender: TObject; const AObjectName, APropName: string; var AReplaceText: string; var AHandled: Boolean);
procedure OnGetFile(Sender: TObject; const AFilename: string; var AText: string; var AHandled: Boolean);
public
class function GetTValueVarAsString(const Value: TValue; const VarName: string; const Processor: TWebStencilsProcessor): String;
procedure Execute(const ViewName: string; const Builder: TStringBuilder); override;
Expand Down Expand Up @@ -152,6 +153,20 @@ function MakeMethodJSON: IInvokable;
end);
end;

procedure TMVCWebStencilsViewEngine.OnGetFile(Sender: TObject; const AFilename: string; var AText: string; var AHandled: Boolean);
var
lFName: String;
begin
AHandled := False;
if TPath.IsRelativePath(AFilename) then
begin
lFName := TPath.Combine(FViewPath, AFilename);
lFName := TPath.ChangeExtension(lfname, FDefaultViewFileExtension);
lFName := TPath.Combine(AppPath, lfname);
AText := TFile.ReadAllText(lfname);
AHandled := True;
end;
end;

procedure TMVCWebStencilsViewEngine.OnGetValue(Sender: TObject; const AObjectName, APropName: string; var AReplaceText: string; var AHandled: Boolean);
var
Expand Down Expand Up @@ -229,6 +244,8 @@ procedure TMVCWebStencilsViewEngine.Execute(const ViewName: string; const Builde
lWebStencilsProcessor.InputFileName := lViewFileName;
lWebStencilsProcessor.PathTemplate := Config[TMVCConfigKey.ViewPath];
lWebStencilsProcessor.WebRequest := WebContext.Request.RawWebRequest;
lWebStencilsProcessor.OnFile := OnGetFile;

if Assigned(ViewModel) then
begin
for lPair in ViewModel do
Expand All @@ -239,6 +256,7 @@ procedure TMVCWebStencilsViewEngine.Execute(const ViewName: string; const Builde
end;
if Assigned(WebContext.LoggedUser) then
begin
lWebStencilsProcessor.UserLoggedIn := True;
lWebStencilsProcessor.UserRoles := WebContext.LoggedUser.Roles.ToString;
end;
if Assigned(FBeforeRenderCallback) then
Expand Down
Loading

0 comments on commit b26a8d6

Please sign in to comment.