guard function
Runs body, turning a thrown Rejection into its own response and
anything else into a 500 with no detail in it.
Generated handlers wrap the handwritten call in this, so a guard clause can
throw a rejection instead of threading a Result all the way back.
return guard(() async {
final todo = await create(user, input);
return jsonResponse(todo.toJson(), status: 201);
});
Implementation
Future<Response> guard(Future<Response> Function() body) async {
try {
return _checkHeaders(await body());
} on Rejection catch (rejection) {
return rejection.intoResponse();
} on HijackException {
// Taking over the socket is how a WebSocket upgrade succeeds, and `shelf`
// signals it by throwing. Catching it would answer 500 on every upgrade.
rethrow;
} on Object catch (error, stack) {
ServerErrors.report(error, stack);
return const Rejection.internal().intoResponse();
}
}