Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add actual type for pharaoh error #109

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading