exceptionHandler function

Middleware exceptionHandler()

Implementation

shelf.Middleware exceptionHandler() {
  return (shelf.Handler handler) {
    return (shelf.Request request) {
      return Future.sync(() => handler(request))
          .then((response) => response)
          .catchError((Object error, StackTrace stackTrace) {
        print("Error: $error, $stackTrace");
        // Make sure this is an exception which is OK to expose to the user.
        // If it is not, return something generic, like a 500.
        // If it is, return the error message formatted in a way
        // that the client can understand (ideally based on request headers).
        if (error is! ShorebirdException) {
          return shelf.Response.internalServerError(
              body: 'Internal Server Error');
        }
        return shelf.Response(
          error.status,
          body: {'message': error.message},
          headers: {HttpHeaders.contentTypeHeader: 'application/json'},
        );
      },
              // test is important to avoid catching HijackExceptions
              // which are used by Shelf for control flow.
              test: (error) =>
                  error is Exception && error is! shelf.HijackException);
    };
  };
}