Fork me on GitHub

Interrupt

TODO Minimal advocacy preamble. And for the rest of the document, there is a lot yet to do. It repeats the `readme.t.js`, a lot, and it is not very meaningful without code examples. The `readme.t.js` and this document should link back and forth.

To learn how to use Interrupt in your program please use the readme.t.js. This is the API documentation for Interrupt. It lays out the rules for Interrupt but it doesn't really cover the finder points of usage.

UserError = Interrupt.create(className[, superClass ][, code | codes{ name: prototype | message } | codes() | codes[] ]*)

This variadic function accepts a required className, followed by an optional superClass followed by zero or many optional error code definitions.

Create a new error class derived from `Interrupt`. The class name is required and must be a valid JavaScript identifier or a dot separated path of valid JavaScript identifiers.

Code definition will register codes for the generated `Interrupt` object and and create associated `Symbols` for each code. Each code must be specified only once or else an exception is raised.

For each code a static property is added to the generated error class with the name of the code and the symbol for the code as a value.

userError = new UserError([ properties{} | [ code | message ] | error | errors[] | stackTraceLimit ]*)

You can specify any of the arguments any number of times. The results are merged to create a final set of options used to create the constructed error. The `error` and `errors` arguments and the `errors` property of a `properties` object will be pushed onto a single array to create the array of nested errors used for the `errors` property of the constructed object. The `'#errors'` property of a `properties` object will also be pushed onto a single construction errors array. All other arguments arguments will override previous value specified if any.

The error constructor will not perform any assertions. It will not raise an excpetion if it incounters invalid arguments. It ignores arguments it cannot interpret or properties in the `properties` argument that are invalid (i.e. null, wrong type, etc.) Any errors encoutered during construction will be reported in the stack trace output in a construction errors section that follows the properties section of the stack trace message. An array of construction errors can also be obtained using `Interrupt.errors(error)`.

The `properties` are used to both specify options and set additional properties on the constructed error. Special properties will not be used to set properties on the constructed error. Additional properties must have valid JavaScript identifier names otherwise they are ignored and construction error is recorded.

Names in a `properties` object prefixed with an underscore `_` will not be set on the constructed error, but they will be available to `sprintf` to format the `message` property of the constructed error.

The `errors` property must be an array and can contain objects of any type, not just instances of `Error`.

The `#type` property is only useful in specifying currying to helper functions. **TODO** Just link to to the `readme.t.js`. To hard to explain.

The `'#errors'` array can be used by helper functions like `assert` to report construction errors encountered prior to calling the error constructor. A construction error should have string `code`, a non-enumberable `symbol` property whose value is a `Symbol` with a display name is the same as the code, a `message`. Any additional properites should be have JavaScript identifier names and JSON serializable values.

UserError.codes

An array containing the names of all the codes for the error.

UserError.<USER_CODE>

A static property for each code defined in the `Interrupt.code()` declarator is defined on the generated error class. This symbol will be used to set the symbol property of constructed errors.

const ConfigError = Interrupt.create('ConfigError', 'IO_ERROR')
assert(typeof ConfigError.IO_ERROR === 'symbol')

Interrupt.Code(object{ code, symbol } | ( code, symbol ))

Construct a code object with an enumerable code name property and a non-enumerable symbol property for the code symbol.

This is a helper function to add additional codes to your constructed errors that you can test by symbol at runtime, but whose symbols will be excluded from JSON serialized output.

Interrupt.OPTIONS

When you construct an options object using Interrupt.options() the '#type' property is set to this symbol to unambiuously identify the object as an options object.

Interrupt.CURRY

A pre-constructed options object for use with user-defined helper functions to indicate that the helper function should be curried. An object with a `'#type'` property whose value is `Interrupt.OPTIONS` is an options object and cannot be mistaken for any other type.

Interrupt.message(error)

If error is an instance of Interrupt, return the sprintf formatted error message without the additional stack trace formatted message. If `error` is any other object return the `message` property of that object.

Interrupt.parse(stack)

Parse a stack trace from any Error instance. Returns an object with the error class, message and an array with the parsed stack trace. If the stack trace is from an instance of Interrupt or the result of calling Interrupt.stringify() on a non-Interrupt Error the resuling object tree will include enumerable Error properties and nested errors.

Interrupt.explode(error)

TODO Why not `null` or `undefined`. What if you pass in `1`. Can't we just JSON stringify and parse something?

TODO Return the component parts and a flag instead of the array encasement.

Converts an Error into an object containing its component parts for serialization in the Interrupt serialization format or else returns an array containing the component parts if the Interrupt serialization would be malformed.

Interrupt.stringify(error)

Serialize the given `error` using the Interrupt serialization format. If `error` is an instance of `Interrupt`, simply return `error.stack`. Otherwise serialize the error using the Interrupt serialization format including enumerable properties and nested errors. If the `Error` properties would create a malformed Interrupt serialization, serialize the `Error` properties as JSON instead.

userError[util.inspect.custom](depth, options)

Implements serialization used by `util.inspect`. `util.inspect` is used to dump the exception to standard error when an Node.js program has an uncaught exception or an unhandled rejection. `util.inspect` would ordinarily duplicate the dispaly of enumerable properties using its own unparsible serialization format. This implementation returns the parsible `error.stack` property of an instance of `Interrupt`.

userError.toString()

Returns the generated class name and the `sprintf` formatted message spearated by a colon. Without overriding it, the full message with properties, nested errors and headings would be returned.