Skip to content

Commit 3cae9e8

Browse files
author
CarlosHe
committed
Compatibility with Lazarus
1 parent 5616f57 commit 3cae9e8

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

Src/Horse.HandleException.pas

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
unit Horse.HandleException;
2-
2+
{$IF DEFINED(FPC)}
3+
{$MODE DELPHI}{$H+}
4+
{$ENDIF}
35
interface
46

5-
uses Horse, Horse.Commons, System.SysUtils;
7+
uses
8+
{$IF DEFINED(FPC)}
9+
SysUtils,
10+
{$ELSE}
11+
System.SysUtils,
12+
{$ENDIF}
13+
Horse, Horse.Commons;
614

7-
procedure HandleException(Req: THorseRequest; Res: THorseResponse; Next: TProc);
15+
procedure HandleException(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)}TNextProc{$ELSE}TProc{$ENDIF});
816

917
implementation
1018

11-
uses System.JSON, System.TypInfo;
19+
uses
20+
{$IF DEFINED(FPC)}
21+
fpjson, TypInfo;
22+
{$ELSE}
23+
System.JSON, System.TypInfo;
24+
{$ENDIF}
25+
26+
procedure SendError(ARes:THorseResponse; AJson: TJSONObject; AStatus: Integer);
27+
begin
28+
ARes.Send<TJSONObject>(AJson).Status(AStatus);
29+
end;
1230

13-
procedure HandleException(Req: THorseRequest; Res: THorseResponse; Next: TProc);
31+
procedure HandleException(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)}TNextProc{$ELSE}TProc{$ENDIF});
1432
var
1533
LJSON: TJSONObject;
1634
begin
@@ -23,34 +41,34 @@ procedure HandleException(Req: THorseRequest; Res: THorseResponse; Next: TProc);
2341
on E: EHorseException do
2442
begin
2543
LJSON := TJSONObject.Create;
26-
LJSON.AddPair('error', E.Error);
44+
LJSON.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}('error', E.Error);
2745

2846
if not E.Title.Trim.IsEmpty then
2947
begin
30-
LJSON.AddPair('title', E.Title);
48+
LJSON.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}('title', E.Title);
3149
end;
3250

3351
if not E.&Unit.Trim.IsEmpty then
3452
begin
35-
LJSON.AddPair('unit', E.&Unit);
53+
LJSON.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}('unit', E.&Unit);
3654
end;
3755

3856
if E.Code <> 0 then
3957
begin
40-
LJSON.AddPair('code', TJSONNumber.Create(E.Code));
58+
LJSON.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}('code', {$IF DEFINED(FPC)}TJSONIntegerNumber{$ELSE}TJSONNumber{$ENDIF}.Create(E.Code));
4159
end;
4260

43-
LJSON.AddPair('type', GetEnumName(TypeInfo(TMessageType), Integer(E.&Type)));
61+
LJSON.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}('type', GetEnumName(TypeInfo(TMessageType), Integer(E.&Type)));
4462

45-
Res.Send<TJSONObject>(LJSON).Status(E.Status);
63+
SendError(Res, LJSON, E.Code);
4664
end;
4765

4866
on E: Exception do
4967
begin
5068
LJSON := TJSONObject.Create;
51-
LJSON.AddPair('error', E.ClassName);
52-
LJSON.AddPair('description', E.Message);
53-
Res.Send<TJSONObject>(LJSON).Status(THTTPStatus.BadRequest);
69+
LJSON.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}('error', E.ClassName);
70+
LJSON.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}('description', E.Message);
71+
SendError(Res, LJSON, Integer(THTTPStatus.BadRequest));
5472
end;
5573
end;
5674
end;

0 commit comments

Comments
 (0)