RequireExtraction<T> extension

Runs an extractor and throws the rejection instead of returning it.

extract hands back a Result because generated code decides what to do with each failure. Hand-written handlers almost always want the same thing, which is to stop and answer with the rejection, and threading a Result through every line to say so buries the handler in casts.

The throw is caught by guard, so a handler wrapped in one reads top to bottom:

Future<Response> readTodo(Request request) => guard(() async {
      final id = await const PathExtractable<String>('id').require(request);
      final repo = await const StateExtractable<TodoRepo>().require(request);
      return jsonResponse(repo.find(id).toJson());
    });

Outside a guard the rejection escapes as an exception, which is the same mistake as letting any other exception escape a handler.

on

Methods

require(Request request) Future<T>

Available on FromRequestParts<T>, provided by the RequireExtraction extension

Extracts the value, throwing the Rejection when extraction fails.