ErrorHandler

api.ErrorHandler
object ErrorHandler

Provides error handling for HTTP routes in a standardised way.

The ErrorHandler wraps existing HTTP routes and ensures that:

  • NotFound errors return a 404 status with a JSON-encoded ErrorResponse.
  • Validation errors (e.g., IllegalArgumentException) return a 400 status.
  • Unexpected errors return a 500 status with a generic message.

This utility promotes consistent error handling across the application.

Attributes

Source
ErrorHandler.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def apply(routes: HttpRoutes[IO]): HttpRoutes[IO]

Wraps the provided HTTP routes with error handling logic.

Wraps the provided HTTP routes with error handling logic.

This function ensures that errors raised during route processing are captured and translated into appropriate HTTP responses with JSON-encoded error messages.

Error Handling:

  • NoSuchElementException: Translates to a 404 Not Found response.
  • IllegalArgumentException: Translates to a 400 Bad Request response.
  • Any other exception: Translates to a 500 Internal Server Error response.

Value parameters

routes

The HttpRoutes[IO] to be wrapped with error handling.

Attributes

Returns

A new HttpRoutes[IO] that handles errors consistently and returns meaningful responses.

Source
ErrorHandler.scala