Skip to content

Exceptions

Since Serinus is a web framework, it has to deal with a lot of different errors that can happen during the request lifecycle. To make it easier to handle these errors, Serinus provides a way to create custom exceptions that can be thrown and caught in the application.

Built-in Exceptions

Serinus provides a set of built-in exceptions that you can use to handle common errors in your application. These exceptions are:

ExceptionDescriptionStatus Code
BadRequestExceptionThrown when the request is invalid.400
UnauthorizedExceptionThrown when the user is not authorized to access the resource.401
ForbiddenExceptionThrown when the user is not allowed to access the resource.403
NotFoundExceptionThrown when the requested resource is not found.404
MethodNotAllowedExceptionThrown when the method is not allowed on the resource.405
ConflictExceptionThrown when there is a conflict with the current state of the resource.409
GoneExceptionThrown when the requested resource is no longer available.410
PreconditionFailedExceptionThrown when the requested resource is no longer available.412
PayloadTooLargeExceptionThrown when the request payload is too large.413
UnsupportedMediaTypeExceptionThrown when the media type is not supported.415
UnprocessableEntityExceptionThrown when the request is valid, but the server cannot process it.422
TooManyRequestsExceptionThrown when the client has sent too many requests in a given amount of time.429
InternalServerErrorExceptionThrown when an internal server error occurs.500
NotImplementedExceptionThrown when the requested feature is not implemented.501
BadGatewayExceptionThrown when the gateway is bad.502
ServiceUnavailableExceptionThrown when the service is unavailable.503
GatewayTimeoutExceptionThrown when the gateway times out.504

Creating a Custom Exception

To create a custom exception, you need to create a class that extends the SerinusException class. The SerinusException class has a constructor that takes a message and a status code as parameters.

dart
import 'package:serinus/serinus.dart';

class MyException extends SerinusException {
  MyException({super.message, super.statusCode = 500});
}

In the MyException class, you can override the message and statusCode properties to customize the exception message and status code.

Built with 💙 and Dart 🎯 | One of the 🐤 of Avesbox