Skip to content

Latest commit

 

History

History
50 lines (44 loc) · 3.67 KB

File metadata and controls

50 lines (44 loc) · 3.67 KB
title serializeError()
order 5

Serializing errors

Stacklyn.serializeError(error: object)

If you've ever been tired of trying to JSON.stringify an error and it was just {}, this function solves all that.

Serialized Properties:

Property What it is Platforms
name Error type (e.g. TypeError or SyntaxError), depending on platform it can be fully absent. All1
message Error message (e.g. "bad thing happened"), depending on platform it can be the only property. All
stack On most platforms, this starts with "name: message\n...", on legacy Opera versions <10 it's "...error description\nBacktrace\n....", the frames are always after the first (or second on Opera <10) \n. additionally the frames are always separated by \n. All
cause On some platforms this is the original error that caused the current error, e.g. "Error: bad thing happened", this is not present on all errors, but standard as of ES2022. All
errors An array of errors from an AggregateError. All
error The error that resulted in suppression. (only in SuppressedError instances) All
suppressed The error that got suppressed during resource disposal. (only in SuppressedError instances) All
toString (method) Typically returns the name and message (e.g. "Error: bad thing happened"). though in some older browsers it looked different. All
code Machine-readable string error code (e.g., "ENOENT", "EADDRINUSE"). Node.js, Bun, Deno
errno System-specific numerical error code (e.g., -2 for ENOENT). Node.js, Bun, Deno
syscall The system call that caused the error (e.g., "open", "connect", "rename", "spawn", etc). Node.js, Bun, Deno
address The network address involved in a network error. Node.js, Bun, Deno
port The network port involved in a network error. Node.js, Bun, Deno
path The file path involved in file system operations (e.g., when a file is not found or when renaming files). Node.js, Bun, Deno
dest The destination path in file system operations (e.g., when renaming or copying files). Node.js, Bun, Deno
spawnargs The arguments passed to spawn(path, spawnArgs) or spawnSync() (imported from child_process), where spawnArgs is an array of arguments passed to the spawned process. Node.js, Bun, Deno
fileName filename of the first frame in the stack trace. Firefox
lineNumber line number of the first frame in the stack trace. Firefox
columnNumber column number of the first frame in the stack trace. Firefox
sourceURL this is the url/location of the first frame. Safari
line line number of the first frame. Safari
column column number of the first frame. Safari
number the HRESULT code of the error Internet Explorer
description the exact same as "message". Edge (Legacy), Internet Explorer
arguments arguments passed to the first function in the stack trace (e.g. ["undef"]) Old Chromium
stacktrace Same as stack in some versions and in Opera 10.50+ it's a firefox-style trace, It was added in Opera 9.50 and removed in Opera 15 with the switch to chromium. Opera
opera#sourceloc Added in Opera 7, Removed in Opera 10.50, gives the line number of the first frame. Opera

Example:

const serialized = Stacklyn.serializeError(error);
console.log(JSON.stringify(serialized, null, 2)); // not empty!

Look at this.

Footnotes

  1. Most platforms have these properties, but some still don't, listing all of them would take too long though.