Skip to content

Commit

Permalink
fix: When sending HTTP 400 Bad Request error message to the client, a…
Browse files Browse the repository at this point in the history
…n error message may now be included in the client side exception. (serverpod#2703)
  • Loading branch information
christerswahn authored Sep 3, 2024
1 parent ba8d1f6 commit a9f5fbf
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/serverpod/lib/src/server/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ class Server {
return;
} else if (result is ResultStatusCode) {
request.response.statusCode = result.statusCode;
if (result.message != null) {
request.response.writeln(result.message);
}
await request.response.close();
return;
} else if (result is ExceptionResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class ServerpodClientException implements Exception {
/// to the server.
class ServerpodClientBadRequest extends ServerpodClientException {
/// Creates a Bad Request Exception
ServerpodClientBadRequest() : super('Bad request', HttpStatus.badRequest);
ServerpodClientBadRequest([String? message])
: super(
'Bad request${message != null && message != '' ? ': $message' : ''}',
HttpStatus.badRequest,
);
}

/// Thrown if the client fails to authenticate and is therefore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dynamic getExceptionFrom({
}

return switch (statusCode) {
HttpStatus.badRequest => ServerpodClientBadRequest(),
HttpStatus.badRequest => ServerpodClientBadRequest(data),
HttpStatus.unauthorized => ServerpodClientUnauthorized(),
HttpStatus.forbidden => ServerpodClientForbidden(),
HttpStatus.notFound => ServerpodClientNotFound(),
Expand Down
2 changes: 1 addition & 1 deletion packages/serverpod_client/test/exception_from_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void main() {
});

test('then the message is a bad request', () {
expect(exception.message, 'Bad request');
expect(exception.message, 'Bad request: malformed data');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ void main() async {
}
expect(clientException, isNotNull);
expect(clientException!.statusCode, equals(400));
expect(
clientException.message,
startsWith('Bad request: '),
);
});
});
}
Expand Down

0 comments on commit a9f5fbf

Please sign in to comment.