Skip to content

feat: Add custom exception handling mechanism#216

Open
yuen-quinn wants to merge 8 commits intovania-dart:devfrom
yuen-quinn:exception
Open

feat: Add custom exception handling mechanism#216
yuen-quinn wants to merge 8 commits intovania-dart:devfrom
yuen-quinn:exception

Conversation

@yuen-quinn
Copy link

Overview

Added a FastAPI-like custom exception handling mechanism that allows users to register custom exception handlers for framework exceptions and third-party library exceptions.

Changes

New Files

  • lib/src/exception/exception_handler.dart - Defines ExceptionHandler<T> and GeneralExceptionHandler abstract classes

Modified Files

  • lib/application.dart - Added exception handler registration methods
    • addExceptionHandler<T>() - Register a single exception handler
    • addExceptionHandlers() - Batch register exception handlers
    • setGeneralExceptionHandler() - Set a general exception handler
  • lib/src/http/request/request_handler.dart - Integrated exception handling logic

Usage

import 'package:vania/src/exception/database_exception.dart';
import 'package:vania/src/exception/exception_handler.dart';
import 'package:vania/src/exception/not_found_exception.dart';
import 'package:vania/src/exception/query_exception.dart';
import 'package:vania/src/exception/validation_exception.dart';
import 'package:vania/src/http/request/request.dart';
import 'package:vania/src/http/response/response.dart';
import 'package:vania/src/service/service_provider.dart';
import 'package:vania/application.dart';

class DatabaseExceptionHandler extends ExceptionHandler<DatabaseException> {
  @override
  Response handle(DatabaseException exception, Request? request) {
    return Response.json({
      'success': false,
      'message': 'Database error occurred',
      'error': exception.message,
    }, 500);
  }
}

class QueryExceptionHandler extends ExceptionHandler<QueryException> {
  @override
  Response handle(QueryException exception, Request? request) {
    return Response.json({
      'success': false,
      'message': 'Query error occurred',
      'error': exception.cause,
    }, 500);
  }
}

class NotFoundExceptionHandler extends ExceptionHandler<NotFoundException> {
  @override
  Response handle(NotFoundException exception, Request? request) {
    return Response.json({
      'success': false,
      'message': exception.message,
    }, 404);
  }
}

class ValidationExceptionHandler extends ExceptionHandler<ValidationException> {
  @override
  Response handle(ValidationException exception, Request? request) {
    return Response.json({
      'success': false,
      'message': 'Validation failed',
      'errors': exception.message,
    }, 422);
  }
}

class ThirdPartyExceptionHandler extends GeneralExceptionHandler {
  @override
  Response? handle(dynamic exception, Request? request) {
    return Response.json({
      'success': false,
      'message': 'An unexpected error occurred',
      'error': exception.toString(),
    }, 500);
  }
}

class AppExceptionServiceProvider extends ServiceProvider {
  @override
  Future<void> boot() async {}

  @override
  Future<void> register() async {
    Application().addExceptionHandlers({
      DatabaseException: DatabaseExceptionHandler(),
      QueryException: QueryExceptionHandler(),
      NotFoundException: NotFoundExceptionHandler(),
      ValidationException: ValidationExceptionHandler(),
    });
    Application().setGeneralExceptionHandler(
      ThirdPartyExceptionHandler(),
    );
  }
}

apple added 8 commits March 1, 2026 12:11
- Add ExceptionHandler<T> and GeneralExceptionHandler interfaces
- Add exception handler registration in Application class
- Integrate exception handlers in RequestHandler and ControllerHandler
- Add example AppExceptionServiceProvider with common exception handlers
- Support both framework exceptions and third-party library exceptions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant