Skip to content

Commit

Permalink
chore: pass response to onError cb (#105)
Browse files Browse the repository at this point in the history
This allows us to keep the current response object by default. You can override it by returning a new instance of Response
  • Loading branch information
codekeyz authored Jan 5, 2024
1 parent 430dbd0 commit f01f099
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/pharaoh/lib/src/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import 'shelf_interop/shelf.dart' as shelf;

part 'core_impl.dart';

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

abstract class Pharaoh implements RouterContract {
factory Pharaoh() => $PharaohImpl();
Expand Down
2 changes: 1 addition & 1 deletion packages/pharaoh/lib/src/core_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class $PharaohImpl extends RouterContract with RouteDefinitionMixin implements P
return forward(httpReq, errorResponse);
}

final result = await _onErrorCb!.call(requestError, request);
final result = await _onErrorCb!.call(requestError, request, response);
return forward(httpReq, result);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/pharaoh/lib/src/http/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ abstract class Response extends Message<shelf.Body?> {

Response status(int code);

Response withBody(Object object);

/// [data] should be json-encodable
Response json(Object? data, {int? statusCode});

Expand Down
3 changes: 3 additions & 0 deletions packages/pharaoh/lib/src/http/response_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class ResponseImpl extends Response {
headers: headers,
);

@override
Response withBody(Object object) => this..body = shelf.Body(object);

@override
ResponseImpl redirect(String url, [int statusCode = HttpStatus.found]) {
headers[HttpHeaders.locationHeader] = url;
Expand Down
10 changes: 8 additions & 2 deletions packages/pharaoh/test/core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ void main() {

test('should use onError callback if provided', () async {
final app = Pharaoh()
..onError((error, req) => Response.create(statusCode: 500, body: 'An error occurred just now'))
..use((req, res, next) => next(res.header('foo', 'bar')))
..onError((error, req, res) => res.status(500).withBody('An error occurred just now'))
..get('/', (req, res) => throw ArgumentError('Some weird error'));

await (await request(app)).get('/').expectStatus(500).expectBody('An error occurred just now').test();
await (await request(app))
.get('/')
.expectStatus(500)
.expectBody('An error occurred just now')
.expectHeader('foo', 'bar')
.test();
});
});
}

0 comments on commit f01f099

Please sign in to comment.