Skip to content

[REFACTOR] Enforce consistent HTTP status code management in exception handlers #220

Closed

Description

@aniebietafia

Problem

In the current GlobalExceptionHandler.java, exception handlers are configured with redundant and potentially conflicting annotations. For example, handleNotFoundException has:

@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<@NonNull ErrorResponse> handleNotFoundException(NotFoundException ex) {
    ...
    return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

In Spring Framework, when a handler returns a ResponseEntity, it completely overrides any @ResponseStatus class-level or method-level annotations. Thus, @ResponseStatus(HttpStatus.NOT_FOUND) is dead code. Furthermore, defining status codes in multiple places invites configuration slip where a developer changes the ResponseEntity status but forgets to update the @ResponseStatus annotation, making the code confusing.

Proposed Solution

Perform a code clean-up on GlobalExceptionHandler.java. Remove all @ResponseStatus annotations from methods that return ResponseEntity. Enforce a rule that all HTTP status code configurations are declared in a single location—ideally directly within the ResponseEntity.status(...) factory call.

User Stories

  • As a backend developer, I want exception handlers to define response codes in a single, consistent way so that the code is readable and free of redundant configuration.

Acceptance Criteria

  • All @ResponseStatus annotations are removed from methods returning ResponseEntity inside GlobalExceptionHandler.java.
  • All custom controller files are audited to ensure they do not mix @ResponseStatus annotations on endpoints that return ResponseEntity.
  • Status codes are configured solely through the return expression.

Tasks

  • Audit GlobalExceptionHandler.java and remove @ResponseStatus on lines 55, 64, 73, 83, 93, 102, 111, 121, 130, 139, 154, 163, 178, and 187.
  • Confirm application behavior during exception handling remains identical.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    apibugdependencies

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions