ExitCodeExceptionMapper class abstract interface

Turns the error that made a runner fail into an exit code.

When an ApplicationRunner throws, the program should usually end with a code that says what went wrong (a missing input file, a database that cannot be reached), and the error often comes from a library that knows nothing about exit codes. Declare a bean of this type: after a failing runner, SpringApplication asks the mappers in @Order order, publishes the first non-zero code as an ExitCodeEvent and keeps it for SpringApplication.exitCodeOf(error).

Mappers are only asked while the context is active, so for a failing runner and not for a start that fails while the beans are created.

@Component()
class ImportExitCodes implements ExitCodeExceptionMapper {
  @override
  int getExitCode(Object exception) =>
      exception is FileSystemException ? 3 : 0;
}

Future<void> main(List<String> args) async {
  try {
    await runApplicationAsync(args);
  } catch (error) {
    exit(SpringApplication.exitCodeOf(error) ?? 1);
  }
}

Spring (Java):

@Bean
ExitCodeExceptionMapper importExitCodes() {
  return exception -> exception instanceof IOException ? 3 : 0;
}

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

getExitCode(Object exception) → int
The exit code for exception, the error thrown by the runner. Return 0 for an error this mapper does not handle, so the next mapper is asked.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited