Skip to content

Commit 72428e6

Browse files
committed
fix(api): enforce camelCase for error codes in responses
Updates the `_mapExceptionToCodeString` function in the error handler middleware to use `camelCase` for all generated error codes (e.g., `INVALID_INPUT` becomes `invalidInput`). This change ensures that all API error responses are fully compliant with the project's strict `camelCase` convention for JSON payloads.
1 parent d429b17 commit 72428e6

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

lib/src/middlewares/error_handler.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Middleware errorHandler() {
3333
statusCode: HttpStatus.badRequest, // 400
3434
body: {
3535
'error': {
36-
'code': 'INVALID_FORMAT',
36+
'code': 'invalidFormat',
3737
'message': 'Invalid data format: ${e.message}',
3838
},
3939
},
@@ -45,7 +45,7 @@ Middleware errorHandler() {
4545
statusCode: HttpStatus.internalServerError, // 500
4646
body: {
4747
'error': {
48-
'code': 'INTERNAL_SERVER_ERROR',
48+
'code': 'internalServerError',
4949
'message': 'An unexpected internal server error occurred.',
5050
// Avoid leaking sensitive details in production responses
5151
// 'details': e.toString(), // Maybe include in dev mode only
@@ -78,17 +78,17 @@ int _mapExceptionToStatusCode(HtHttpException exception) {
7878
/// Maps HtHttpException subtypes to consistent error code strings.
7979
String _mapExceptionToCodeString(HtHttpException exception) {
8080
return switch (exception) {
81-
InvalidInputException() => 'INVALID_INPUT',
82-
AuthenticationException() => 'AUTHENTICATION_FAILED',
83-
BadRequestException() => 'BAD_REQUEST',
84-
UnauthorizedException() => 'UNAUTHORIZED',
85-
ForbiddenException() => 'FORBIDDEN',
86-
NotFoundException() => 'NOT_FOUND',
87-
ServerException() => 'SERVER_ERROR',
88-
OperationFailedException() => 'OPERATION_FAILED',
89-
NetworkException() => 'NETWORK_ERROR',
90-
ConflictException() => 'CONFLICT',
91-
UnknownException() => 'UNKNOWN_ERROR',
92-
_ => 'UNKNOWN_ERROR', // Default
81+
InvalidInputException() => 'invalidInput',
82+
AuthenticationException() => 'authenticationFailed',
83+
BadRequestException() => 'badRequest',
84+
UnauthorizedException() => 'unauthorized',
85+
ForbiddenException() => 'forbidden',
86+
NotFoundException() => 'notFound',
87+
ServerException() => 'serverError',
88+
OperationFailedException() => 'operationFailed',
89+
NetworkException() => 'networkError',
90+
ConflictException() => 'conflict',
91+
UnknownException() => 'unknownError',
92+
_ => 'unknownError', // Default
9393
};
9494
}

0 commit comments

Comments
 (0)