From f01f09910f86d1a9f07281272dfcd701de865ac8 Mon Sep 17 00:00:00 2001 From: Chima Precious Date: Fri, 5 Jan 2024 04:21:57 +0000 Subject: [PATCH] chore: pass response to onError cb (#105) This allows us to keep the current response object by default. You can override it by returning a new instance of Response --- packages/pharaoh/lib/src/core.dart | 2 +- packages/pharaoh/lib/src/core_impl.dart | 2 +- packages/pharaoh/lib/src/http/response.dart | 2 ++ packages/pharaoh/lib/src/http/response_impl.dart | 3 +++ packages/pharaoh/test/core_test.dart | 10 ++++++++-- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/pharaoh/lib/src/core.dart b/packages/pharaoh/lib/src/core.dart index 6cbff75c..2f9b7dd0 100644 --- a/packages/pharaoh/lib/src/core.dart +++ b/packages/pharaoh/lib/src/core.dart @@ -22,7 +22,7 @@ import 'shelf_interop/shelf.dart' as shelf; part 'core_impl.dart'; -typedef OnErrorCallback = FutureOr Function(Object error, Request req); +typedef OnErrorCallback = FutureOr Function(Object error, Request req, Response res); abstract class Pharaoh implements RouterContract { factory Pharaoh() => $PharaohImpl(); diff --git a/packages/pharaoh/lib/src/core_impl.dart b/packages/pharaoh/lib/src/core_impl.dart index e9306829..e0d598b0 100644 --- a/packages/pharaoh/lib/src/core_impl.dart +++ b/packages/pharaoh/lib/src/core_impl.dart @@ -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); } diff --git a/packages/pharaoh/lib/src/http/response.dart b/packages/pharaoh/lib/src/http/response.dart index bd87c51e..e774e54a 100644 --- a/packages/pharaoh/lib/src/http/response.dart +++ b/packages/pharaoh/lib/src/http/response.dart @@ -38,6 +38,8 @@ abstract class Response extends Message { Response status(int code); + Response withBody(Object object); + /// [data] should be json-encodable Response json(Object? data, {int? statusCode}); diff --git a/packages/pharaoh/lib/src/http/response_impl.dart b/packages/pharaoh/lib/src/http/response_impl.dart index bab2b326..595aab0b 100644 --- a/packages/pharaoh/lib/src/http/response_impl.dart +++ b/packages/pharaoh/lib/src/http/response_impl.dart @@ -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; diff --git a/packages/pharaoh/test/core_test.dart b/packages/pharaoh/test/core_test.dart index 342ea08b..e70fffec 100644 --- a/packages/pharaoh/test/core_test.dart +++ b/packages/pharaoh/test/core_test.dart @@ -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(); }); }); }