mapError
The mapError() function extracts information from an Error object as a human-readable array of strings. This includes the error name, message, and a call stack.
If --enable-stack-traces is specified during the build, the call stack will be mapped using source maps.
If --enable-stack-traces is specified and --exclude-sources is not specified during the build, then this will also include a code dump of neighboring lines of user code.
Syntax
mapError(error)
Parameters
error_: Error _ or string- The error to retrieve information about. If a
stringis provided, then it is first converted to anError.
- The error to retrieve information about. If a
Return value
Returns an array of strings.
Examples
In this example, build the application using the --enable-stack-traces flag.
addEventListener('fetch', e => e.respondWith(handler(e)));
async function handler(event) {
try {
throw new TypeError('foo');
} catch (err) {
console.error(mapError(err));
}
return new Response('ok');
}
The following is output to the error log.
TypeError: foo
at handler (src/index.ts:4:11)
1 | addEventListener('fetch', e => e.respondWith(handler(e)));
2 | async function handler(event) {
3 | try {
> 4 | throw new TypeError('foo');
^
5 | } catch (err) {
6 | console.error(mapError(err));
7 | }
at src/index.ts:1:45