Handling HTTP Errors and Custom Error Pages in Micronaut

When building web applications or REST APIs, things can go wrong:

  • Client sends invalid data.
  • Requested resource is not found.
  • Server encounters an unexpected error.

Handling errors gracefully is extremely important.

Micronaut provides simple yet powerful ways to:

  • Handle HTTP errors.
  • Customize error responses.
  • Create user-friendly error pages.

In this post, you’ll learn HTTP Errors and Custom Error Pages in Micronaut step-by-step with simple examples.

Handling HTTP Errors and Custom Error Pages in Micronaut

Why Proper Error Handling Matters

  • Better User Experience: Users get clear messages instead of cryptic errors.
  • Consistent API responses: Clients (mobile apps, frontends, etc.) know what to expect.
  • Easier Debugging: Well-structured error info helps you fix issues faster.
  • Security: Avoid exposing sensitive details accidentally.

Types of Errors

TypeExample
Client Errors (4xx)400 Bad Request, 404 Not Found
Server Errors (5xx)500 Internal Server Error
Custom Business ErrorsValidation failed, Resource already exists

Micronaut allows you to handle all of these easily.

Default Error Handling in Micronaut

Micronaut automatically handles many HTTP errors.
For example:

  • If you access a non-existing route → 404 Not Found
  • If a request body is invalid → 400 Bad Request

By default, Micronaut returns a JSON response like:

But usually, you want custom error responses that fit your app.

Scenario Example: School Management System

We are building a simple REST API for schools.
If a student with a given ID is not found, we want to return a custom error message.

Step 1: Create a Micronaut Project

Step 2: Create a Student Class

Step 3: Create a Custom Exception

We’ll throw this exception when a student is not found.

Step 4: Create a Controller

✅ If student is not found, we throw our custom exception.

Step 5: Handle the Exception Globally

Now let’s create a global exception handler.

✅ Explanation:

  • We implement ExceptionHandler.
  • Return a custom JSON error response.

Step 6: Create ErrorResponse Class

This class defines the structure of our custom error message.

Step 7: Test the Error Handling

Run your app:

mvn mn:run

Success case:

http://localhost:8080/students/1

Error case:

http://localhost:8080/students/2

🎯 Custom error handling works perfectly!

Bonus: Handle All Other Exceptions Globally

You can even create a global fallback handler:

Optional: Custom Error Pages (For Web Apps)

For web applications (not APIs), you can also define custom HTML error pages.

Example:

  • Create a file:
    src/main/resources/views/errors/404.html

✅ Micronaut will automatically serve this page on 404 errors for browser clients.

Summary Table

FeatureMicronaut
Global Exception HandlingExceptionHandler interface
Custom JSON ErrorCreate response DTO
Handle 404, 500, etc.Fully supported
Custom Error PagesServe HTML files under /views/errors

Conclusion

  • ✅ HTTP Errors and Custom Error Pages in Micronaut are easy to handle
  • ✅ Use ExceptionHandler to catch and handle exceptions globally.
  • ✅ Customize both API responses and web error pages.
  • ✅ Your users and API clients will love your clean error messages!

Further Reading

  1. Micronaut Exception Handling Official Docs
    👉 https://docs.micronaut.io/latest/guide/#exceptionHandler
  2. Micronaut Views and Error Pages
    👉 https://docs.micronaut.io/latest/guide/#views