Skip to content

Commit 0ca93dd

Browse files
Code improvements
1 parent 2beabec commit 0ca93dd

File tree

6 files changed

+115
-41
lines changed

6 files changed

+115
-41
lines changed

boss-lock.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"hash": "830981b993a8554a72d15f4378c1a4cc",
3-
"updated": "2023-03-09T23:16:37.9915349-03:00",
3+
"updated": "2023-12-28T09:51:18.5301133-03:00",
44
"installedModules": {
55
"github.com/hashload/horse": {
66
"name": "horse",
7-
"version": "3.1.0",
8-
"hash": "fc3b8eefb46c1a3b387e86ca46a9faa1",
7+
"version": "3.1.5",
8+
"hash": "3824f65f99e511ba73d3fdd2f6d16413",
99
"artifacts": {},
1010
"failed": false,
1111
"changed": false
1212
},
1313
"github.com/hashload/jhonson": {
1414
"name": "jhonson",
15-
"version": "1.1.5",
16-
"hash": "b4efa1372fdf1109fbaf5a625b702f90",
15+
"version": "1.1.8",
16+
"hash": "62d5763101381ad8aeec10773544b1dc",
1717
"artifacts": {},
1818
"failed": false,
1919
"changed": false

boss.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"mainsrc": "src/",
77
"projects": [],
88
"dependencies": {
9-
"github.com/hashload/horse": "^3.1.0",
10-
"github.com/hashload/jhonson": "^1.1.5"
9+
"github.com/hashload/horse": "^3.1.4",
10+
"github.com/hashload/jhonson": "^1.1.8"
1111
}
1212
}

samples/delphi/samples.dpr

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,56 @@ program samples;
33
{$APPTYPE CONSOLE}
44
{$R *.res}
55

6-
uses Horse, Horse.Jhonson, Horse.HandleException, System.SysUtils;
6+
uses Horse, Horse.Jhonson, Horse.HandleException, System.SysUtils, System.JSON;
77

88
begin
9+
{$region 'Example 01: Handle-exception is responsible for notifying the client (Default)'}
910
THorse
1011
.Use(Jhonson)
1112
.Use(HandleException);
13+
{$endregion}
14+
15+
{$region 'Example 02: Handle-exception is responsible for notifying the client using the TInterceptExceptionCallback Callback'}
16+
// THorse
17+
// .Use(Jhonson)
18+
// .Use(HandleException(
19+
// procedure(const E: Exception; const Req: THorseRequest; const Res: THorseResponse; var ASendException: Boolean)
20+
// var
21+
// LGUID: TGUID;
22+
// LMessage: string;
23+
// begin
24+
// CreateGUID(LGUID);
25+
// LMessage := Format('ID: %s - Message: %s', [GUIDToString(LGUID), E.Message]);
26+
// Writeln(LMessage);
27+
// end));
28+
{$endregion}
29+
30+
{$region 'Example 03: Developer is responsible for notifying the client using the TInterceptExceptionCallback Callback'}
31+
// THorse
32+
// .Use(Jhonson)
33+
// .Use(HandleException(
34+
// procedure(const E: Exception; const Req: THorseRequest; const Res: THorseResponse; var ASendException: Boolean)
35+
// var
36+
// LGUID: TGUID;
37+
// LMessage: string;
38+
// begin
39+
// ASendException := False;
40+
// CreateGUID(LGUID);
41+
// LMessage := Format('ID: %s - Message: %s', [GUIDToString(LGUID), E.Message]);
42+
// Writeln(LMessage);
43+
// Res.Send<TJSONObject>(TJSONObject.Create.AddPair('myCustomError', E.Message)).Status(THTTPStatus.InternalServerError);
44+
// end));
45+
{$endregion}
1246

1347
THorse.Get('/ping',
1448
procedure(Req: THorseRequest; Res: THorseResponse)
1549
begin
1650
raise EHorseException.New.Error('My Error!');
1751
end);
1852

19-
THorse.Listen(9000);
53+
THorse.Listen(9000,
54+
procedure
55+
begin
56+
Writeln('Server is running on port ' + THorse.Port.ToString);
57+
end);
2058
end.

samples/lazarus/Console.lpi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<CONFIG>
33
<ProjectOptions>
4-
<Version Value="11"/>
4+
<Version Value="12"/>
55
<PathDelim Value="\"/>
66
<General>
77
<Flags>
88
<MainUnitHasCreateFormStatements Value="False"/>
99
<MainUnitHasTitleStatement Value="False"/>
1010
<MainUnitHasScaledStatement Value="False"/>
11+
<CompatibilityMode Value="True"/>
1112
</Flags>
1213
<SessionStorage Value="InProjectDir"/>
13-
<MainUnit Value="0"/>
1414
<Title Value="Console"/>
1515
<UseAppBundle Value="False"/>
1616
<ResourceType Value="res"/>
@@ -24,7 +24,6 @@
2424
</PublishOptions>
2525
<RunParams>
2626
<FormatVersion Value="2"/>
27-
<Modes Count="0"/>
2827
</RunParams>
2928
<Units Count="1">
3029
<Unit0>
@@ -44,6 +43,11 @@
4443
<OtherUnitFiles Value="..\..\src;..\..\modules\horse\src;..\..\modules\jhonson\src"/>
4544
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
4645
</SearchPaths>
46+
<Linking>
47+
<Debugging>
48+
<DebugInfoType Value="dsDwarf2Set"/>
49+
</Debugging>
50+
</Linking>
4751
<Other>
4852
<CustomOptions Value="-dUseCThreads"/>
4953
</Other>

samples/lazarus/Console.lpr

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{$IFDEF UNIX}{$IFDEF UseCThreads}
77
cthreads,
88
{$ENDIF}{$ENDIF}
9+
fpjson,
910
Horse,
1011
Horse.Jhonson,
1112
Horse.HandleException,
@@ -16,10 +17,49 @@ procedure GetPing(Req: THorseRequest; Res: THorseResponse; Next: TNextProc);
1617
raise EHorseException.New.Error('My Error!');
1718
end;
1819

20+
//procedure Example02(const E: Exception; const Req: THorseRequest; const Res: THorseResponse; var ASendException: Boolean);
21+
//var
22+
// LGUID: TGUID;
23+
// LMessage: string;
24+
//begin
25+
// CreateGUID(LGUID);
26+
// LMessage := Format('ID: %s - Message: %s', [GUIDToString(LGUID), E.Message]);
27+
// Writeln(LMessage);
28+
//end;
29+
30+
//procedure Example03(const E: Exception; const Req: THorseRequest; const Res: THorseResponse; var ASendException: Boolean);
31+
//var
32+
// LGUID: TGUID;
33+
// LMessage: string;
34+
// LJSON: TJSONObject;
35+
//begin
36+
// ASendException := False;
37+
// CreateGUID(LGUID);
38+
// LMessage := Format('ID: %s - Message: %s', [GUIDToString(LGUID), E.Message]);
39+
// Writeln(LMessage);
40+
// LJSON := TJSONObject.Create;
41+
// LJSON.Add('myCustomError', E.Message);
42+
// Res.Send<TJSONObject>(LJSON).Status(THTTPStatus.InternalServerError);
43+
//end;
44+
1945
begin
46+
{$region 'Example 01: Handle-exception is responsible for notifying the client (Default)'}
2047
THorse
2148
.Use(Jhonson)
2249
.Use(HandleException);
50+
{$endregion}
51+
52+
{$region 'Example 02: Handle-exception is responsible for notifying the client using the TInterceptExceptionCallback Callback'}
53+
//THorse
54+
// .Use(Jhonson)
55+
// .Use(HandleException(Example02));
56+
{$endregion}
57+
58+
{$region 'Example 03: Developer is responsible for notifying the client using the TInterceptExceptionCallback Callback'}
59+
//THorse
60+
// .Use(Jhonson)
61+
// .Use(HandleException(Example03));
62+
{$endregion}
2363

2464
THorse.Get('/ping', GetPing);
2565

src/Horse.HandleException.pas

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
unit Horse.HandleException;
22

33
{$IF DEFINED(FPC)}
4-
{$MODE DELPHI}{$H+}
4+
{$MODE DELPHI}{$H+}
55
{$ENDIF}
66

77
interface
88

99
uses
1010
{$IF DEFINED(FPC)}
11-
SysUtils, fpjson,
11+
SysUtils,
12+
fpjson,
13+
TypInfo,
1214
{$ELSE}
13-
System.SysUtils, System.JSON,
15+
System.SysUtils,
16+
System.JSON,
17+
System.TypInfo,
1418
{$ENDIF}
15-
Horse, Horse.Commons;
19+
Horse,
20+
Horse.Commons;
1621

1722
type
1823
{$IF DEFINED(FPC)}
19-
TInterceptExceptionCallback = {$IF DEFINED(HORSE_FPC_FUNCTIONREFERENCES)}reference to {$ENDIF}procedure(AException: Exception; AResponse: THorseResponse; var ASendException: Boolean);
24+
TInterceptExceptionCallback = procedure(const E: Exception; const Req: THorseRequest; const Res: THorseResponse; var ASendException: Boolean);
2025
{$ELSE}
21-
TInterceptExceptionCallback = reference to procedure(AException: Exception; AResponse: THorseResponse; var ASendException: Boolean);
26+
TInterceptExceptionCallback = reference to procedure(const E: Exception; const Req: THorseRequest; const Res: THorseResponse; var ASendException: Boolean);
2227
{$ENDIF}
2328

2429
function HandleException: THorseCallback; overload;
2530
function HandleException(const ACallback: TInterceptExceptionCallback): THorseCallback; overload;
26-
procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)}TNextProc{$ELSE}TProc{$ENDIF});
27-
28-
function FormatExceptionJSON(AException: Exception): TJSONObject;
2931

3032
implementation
3133

32-
uses
33-
{$IF DEFINED(FPC)}
34-
TypInfo;
35-
{$ELSE}
36-
System.TypInfo;
37-
{$ENDIF}
38-
3934
var
4035
InterceptExceptionCallback: TInterceptExceptionCallback = nil;
4136

@@ -44,27 +39,27 @@ procedure SendException(ARes: THorseResponse; AJson: TJSONObject; const AStatus:
4439
ARes.Send<TJSONObject>(AJson).Status(AStatus);
4540
end;
4641

47-
function FormatExceptionJSON(AException: Exception): TJSONObject;
42+
function FormatExceptionJSON(const E: Exception): TJSONObject;
4843
var
4944
LEHorseException: EHorseException;
5045
begin
51-
if (AException is EHorseException) then
46+
if (E is EHorseException) then
5247
begin
53-
LEHorseException := (AException as EHorseException);
48+
LEHorseException := (E as EHorseException);
5449
Result := {$IF DEFINED(FPC)}GetJSON(LEHorseException.ToJSON) as TJSONObject{$ELSE}TJSONObject.ParseJSONValue(LEHorseException.ToJSON) as TJSONObject{$ENDIF};
5550
end
5651
else
5752
begin
5853
Result := TJSONObject.Create;
59-
Result.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}('error', AException.Message);
54+
Result.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}('error', E.Message);
6055
end;
6156
end;
6257

6358
procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)}TNextProc{$ELSE}TProc{$ENDIF});
6459
var
6560
LJSON: TJSONObject;
6661
LStatus: Integer;
67-
lSendException: Boolean;
62+
LSendException: Boolean;
6863
begin
6964
try
7065
Next();
@@ -74,24 +69,21 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED
7469
if (E is EHorseCallbackInterrupted) then
7570
raise;
7671

77-
lSendException := True;
72+
LSendException := True;
7873
if Assigned(InterceptExceptionCallback) then
79-
InterceptExceptionCallback(E, Res, lSendException);
74+
InterceptExceptionCallback(E, Req, Res, LSendException);
8075

81-
if not lSendException then
76+
if not LSendException then
8277
Exit;
8378

79+
LJSON := FormatExceptionJSON(E);
8480
if (E is EHorseException) then
85-
begin
86-
LJSON := FormatExceptionJSON(E);
87-
SendException(Res, LJSON, Integer(EHorseException(E).Status));
88-
end
81+
SendException(Res, LJSON, Integer(EHorseException(E).Status))
8982
else
9083
begin
9184
LStatus := Res.Status;
9285
if (LStatus < Integer(THTTPStatus.BadRequest)) then
9386
LStatus := Integer(THTTPStatus.InternalServerError);
94-
LJSON := FormatExceptionJSON(E);
9587
SendException(Res, LJSON, LStatus);
9688
end;
9789
end;

0 commit comments

Comments
 (0)