11unit Horse.HandleException;
22
33{ $IF DEFINED(FPC)}
4- { $MODE DELPHI}{ $H+}
4+ { $MODE DELPHI}{ $H+}
55{ $ENDIF}
66
77interface
88
99uses
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
1722type
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
2429function HandleException : THorseCallback; overload;
2530function 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
3032implementation
3133
32- uses
33- { $IF DEFINED(FPC)}
34- TypInfo;
35- { $ELSE}
36- System.TypInfo;
37- { $ENDIF}
38-
3934var
4035 InterceptExceptionCallback: TInterceptExceptionCallback = nil ;
4136
@@ -44,27 +39,27 @@ procedure SendException(ARes: THorseResponse; AJson: TJSONObject; const AStatus:
4439 ARes.Send<TJSONObject>(AJson).Status(AStatus);
4540end ;
4641
47- function FormatExceptionJSON (AException : Exception): TJSONObject;
42+ function FormatExceptionJSON (const E : Exception): TJSONObject;
4843var
4944 LEHorseException: EHorseException;
5045begin
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 ;
6156end ;
6257
6358procedure Middleware (Req: THorseRequest; Res: THorseResponse; Next: { $IF DEFINED(FPC)} TNextProc{ $ELSE} TProc{ $ENDIF} );
6459var
6560 LJSON: TJSONObject;
6661 LStatus: Integer;
67- lSendException : Boolean;
62+ LSendException : Boolean;
6863begin
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