shelf_controller_flow_manager 1.0.2 copy "shelf_controller_flow_manager: ^1.0.2" to clipboard
shelf_controller_flow_manager: ^1.0.2 copied to clipboard

A simple application flow management tool in case of an exception or in its normal flow

Which is? #

ShelfControllerFlowManager is a utility package to handle the application's request and response flow, handling the normal application flow as well as the exception flow

Usage #

A simple usage example:

class PersonController {
  final ShelfRequestResponse2JsonUtility<Person> shelfRequestResponse2JsonUtility =  ShelfRequestResponse2JsonUtility<Person>(PersonFactory());
  final ShelfControllerFlowManager shelfControllerFlowManager = ShelfControllerFlowManager();

  @Route.post('/persons')
  FutureOr<Response> post(Request request) {
    return shelfControllerFlowManager.call(
      request: request,
      normalExecutionFlow: (request) async {
        final body = shelfRequestResponse2JsonUtility.body(request);
        return shelfRequestResponse2JsonUtility.ok(body);
      },
    );
  }
}

Automatically by default the function (callback) executed in the normal flow is invoked, in case of an exception depending on whether the exception thrown internally is a BadRequestException, InternalServerErrorException or UnauthorizedException (Other exceptions will be programmed later) it will throw a different response json for each of the possible exceptions

Important

  • All thrown exceptions must extend the ResponseException class
  • It is possible to configure the call method with a custom ExceptionHandler, with its custom exceptions
class CustomException extends ErrorResponseException {
  CustomException() : super(444, message: 'Custom Exception');
}

class PersonController {
  final ShelfRequestResponse2JsonUtility<Person>
      shelfRequestResponse2JsonUtility =
      ShelfRequestResponse2JsonUtility<Person>(PersonFactory());
  final ShelfControllerFlowManager shelfControllerFlowManager =
      ShelfControllerFlowManager();

  @Route.post('/persons')
  FutureOr<Response> post(Request request) {
    return shelfControllerFlowManager.call(
      request: request,
      normalExecutionFlow: (request) async {
        final body = shelfRequestResponse2JsonUtility.body(request);
        return shelfRequestResponse2JsonUtility.ok(body);
      },
      onExceptionHandler: ExceptionHandler(
        {
          CustomException: (ErrorResponseException exception) async =>
              exception.response,
          ...ExceptionHandler.defaultHandlers,
        },
      ),
    );
  }
}

0
likes
110
pub points
0%
popularity

Publisher

unverified uploader

A simple application flow management tool in case of an exception or in its normal flow

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

shelf

More

Packages that depend on shelf_controller_flow_manager