Exception Filter¶
Exception filters are used to catch exceptions your application throws and do something with them. For example, you might want to catch a 403 http error and display an unauthorized message to the user.
-
Catch
(Exception)¶ Creates a Catch decorator.
Arguments: - extends Constructor | undefined Exception (TError) – The exception type to catch. By omitting this parameter, the decorated exception filter will be a catch-all filter.
Returns: (Anonymous function) – The decorator used to designate a class as an Exception Filter.
-
class
ExceptionFilter
(T)¶ interface, exported from
ommander-mv
An exception filter for errors of a particular type. Use this interface to remember to implement the catch method.
Arguments: - T – The type of error to catch.
-
ExceptionFilter.
catch
(exception)¶ Called when catching an exception of the correct type.
Arguments: - exception (T) – The exception that was caught.
Important
If multiple exception filters would catch an exception, only the first one
will catch it based on the order the exception filters are given in
ContextInitialization.providers
. Therefore, when using a catch-all
exception filter, it should generally be the last exception filter in
ContextInitialization.providers
.
Example¶
This example will catch UnauthorizedError, log the error, and print “Error unauthorized” to the console.
1 2 3 4 5 6 7 8 9 10 11 | @Catch(UnauthorizedError)
export class UnauthorizedErrorFilter {
constructor({ logService }) {
this.logger = logService
}
catch(error) {
this.logger.error(error)
console.error("Error unauthorized")
}
}
|