Error Handling
func separates registration problems, runtime handler errors, and
runtime errors that print a default message. Add @CommandError()
when you want one place to format runtime failures.
@CommandError() only receives F_RUNTIME and
F_RUNTIME_PRINT errors. F_SYSTEM errors are not
delivered to user handlers; once a system error happens, the program stops.
import { CommandError, CommandErrorProvider } from 'func'
@CommandError()
export class ErrorHandler {
constructor(private error: CommandErrorProvider) {
console.error(error.message)
error.preventDefaultPrint()
}
} Runtime Errors
F_RUNTIME is the main runtime error family. It represents failures
that happen while a matched handler is running.
| Code | Meaning |
|---|---|
F_RUNTIME_HANDLER_ERROR | A command, option, major, missing, or error handler threw an error. |
Runtime-print Errors
F_RUNTIME_PRINT is a runtime error family with an extra default
behavior: func prints the message to stderr. A
CommandErrorProvider can handle the error and call
preventDefaultPrint() to block that default print.
import { CommandError, CommandErrorProvider } from 'func'
@CommandError()
export class ErrorHandler {
constructor(private error: CommandErrorProvider) {
if (error.level !== 'runtime-print') return
console.error(`Invalid input: ${error.message}`)
error.preventDefaultPrint()
}
}
In this example, your handler prints a custom message and then prevents
func from printing the original runtime-print message again.
| Code | Meaning |
|---|---|
F_RUNTIME_PRINT_MULTIPLE_OPTIONS | More than one global option was invoked at once. |
F_RUNTIME_PRINT_PARSE | The command-line input could not be parsed. |
F_RUNTIME_PRINT_UNKNOWN_OPTION | The user passed an option that is not declared for the current mode. |
System Errors
System errors indicate invalid command definitions, duplicate registrations,
or injection configuration problems. They happen before user error handlers
can safely run, so func throws them immediately and interrupts the
program. Fix these during development before publishing your CLI.
| Code | Meaning |
|---|---|
F_SYSTEM_DUPLICATE_HANDLER | Two handlers declare the same command or option token. |
F_SYSTEM_MISSING_PROVIDER | A constructor asks for an unknown provider class. |
F_SYSTEM_MISSING_PARAM_TYPES | Constructor metadata is missing. Enable decorators and emitDecoratorMetadata. |
F_SYSTEM_UNSUPPORTED_ARRAY_TYPE | Use [String] for repeated values instead of Array. |