Skip to content

Commit

Permalink
chore: Add type for pharaoh error (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
codekeyz authored Jan 30, 2024
1 parent cd3e57e commit 2827a8a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
7 changes: 6 additions & 1 deletion packages/pharaoh/lib/src/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ import 'shelf_interop/shelf.dart' as shelf;

part 'core_impl.dart';

typedef PharaohError = ({Object exception, StackTrace trace});

typedef OnErrorCallback = FutureOr<Response> Function(
Object error, Request req, Response res);
PharaohError error,
Request req,
Response res,
);

abstract class Pharaoh implements RouterContract {
factory Pharaoh() => $PharaohImpl();
Expand Down
18 changes: 10 additions & 8 deletions packages/pharaoh/lib/src/core_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,28 @@ class $PharaohImpl extends RouterContract

final request = Request.from(httpReq);
final response = Response.create();

late ({Object error, StackTrace trace}) requestError;
late PharaohError requestError;

try {
final result = await resolveAndExecuteHandlers(request, response);
return forward(httpReq, result.res);
} catch (error, trace) {
requestError = (error: error, trace: trace);
requestError = (exception: error, trace: trace);
}

if (_onErrorCb == null) {
final status = requestError.error is SpannerRouteValidatorError
final status = requestError.exception is SpannerRouteValidatorError
? HttpStatus.unprocessableEntity
: HttpStatus.internalServerError;
return forward(
httpReq,
response.json({
'error': requestError.error.toString(),
'trace': requestError.trace.toString()
}, statusCode: status),
response.json(
{
'error': requestError.exception.toString(),
'trace': requestError.trace.toString()
},
statusCode: status,
),
);
}

Expand Down

0 comments on commit 2827a8a

Please sign in to comment.