custom-exception-handling

Implement custom domain exceptions and a global REST exception handler in Spring Boot applications.

Updated Apr 18, 2026
One-click install
npx skills add https://github.com/heiherilala-vawd/ompany-Management --skill custom-exception-handling-heiherilala-vawd
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: custom-exception-handling
Source: https://github.com/heiherilala-vawd/ompany-Management/tree/main/.agents/skills/custom-exception-handling
Command: npx skills add https://github.com/heiherilala-vawd/ompany-Management --skill custom-exception-handling-heiherilala-vawd

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Spring Boot applications without centralized exception handling return inconsistent error responses, leak stack traces, and force developers to duplicate try-catch logic across controllers. This Skill provides a complete pattern for custom domain exceptions mapped to standardized HTTP error responses. ## Core Features & Use Cases - Base Exception Hierarchy: Create a BaseException with ExceptionType (CLIENT_EXCEPTION / SERVER_EXCEPTION) plus eight business exceptions covering HTTP 400, 401, 403, 404, 409, 429, 500, and 501. - Global REST Handler: A @RestControllerAdvice class that intercepts custom exceptions, Spring validation errors, security exceptions, and database lock exceptions, returning a consistent { "type", "message" } JSON body. - OpenAPI Integration: Add ExceptionResponse schemas to api.yml and reference standardized error responses on every endpoint. - Use Case: When building a Spring Boot REST API with JWT security and @PreAuthorize role checks, use this Skill so that AccessDeniedException automatically becomes a 403 response and NotFoundException thrown in services becomes a clean 404 JSON body. ## Quick Start Implement custom exception handling with a global REST exception handler in my Spring Boot project following the clean architecture pattern.

Frequently Asked Questions about custom-exception-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create a global exception handler in Spring Boot?

Create a class annotated with @RestControllerAdvice and define @ExceptionHandler methods for each exception type. Each method returns a ResponseEntity containing a standardized error body with the appropriate HTTP status code.

How to map custom Java exceptions to HTTP status codes?

Define custom exceptions extending a base RuntimeException, then write one @ExceptionHandler method per exception in the advice class. For example, NotFoundException maps to 404 NOT_FOUND and BadRequestException maps to 400 BAD_REQUEST.

Does @RestControllerAdvice work with Spring Security @PreAuthorize?

Yes, with @EnableMethodSecurity enabled, AccessDeniedException and AuthorizationDeniedException thrown by @PreAuthorize checks are caught by the advice handler and converted to 403 FORBIDDEN responses.

How do I handle database lock exceptions in Spring Boot REST APIs?

Catch LockAcquisitionException, CannotAcquireLockException, and OptimisticLockException in a single handler and convert them to 429 TOO_MANY_REQUESTS responses, since lock contention indicates temporary overload rather than a server bug.

What log level should I use for 4xx versus 5xx errors?

Log 4xx client errors at info level since they reflect client mistakes, not server bugs. Use warn for 429 and lock exceptions, and error for 5xx responses since those indicate unexpected server-side failures.

How do I document error responses in an OpenAPI specification?

Define an ExceptionResponse schema with type and message properties in the components section of api.yml, then create per-status schemas using allOf inheritance and reference them in each endpoint's responses block.