KO
|
EN
gitlite β search
Search
#python
#java
#python3
#arduino
#golang
#machine-learning
#rust
#html
#flask
#javascript
#seismology
#nodejs
yerror
β 13
Open GitHub β
π¨ It helps to know Y you got an Error.
Download README (.md)
Explore Similar Repositories
db_meter_library
:
Arduino library to implement a db meter with a analog sound sensor.
staff
:
Web Services Framework for C++ based on Axis2/C
markdown-it-named-headers
:
Plugin for markdown-it markdown parser. Adds name attributes to header tags in output.
gensum
:
Powerful checksum generator!
biogenerator
:
biogenerator
// repository documentation
Was this content helpful?
β 0
(0 ratings)
Select Rating:
β
β
β
β
β
Submit Feedback
Recent Feedback
×
Download README
Do you want to download the
README.md
file for
yerror
?
Download (.md)
[//]: # ( ) [//]: # (This file is automatically generated by a `metapak`) [//]: # (module. Do not change it except between the) [//]: # (`content:start/end` flags, your changes would) [//]: # (be overridden.) [//]: # ( ) # yerror > It helps to know why you got an error. [](https://github.com/nfroidure/yerror/blob/main/LICENSE) [//]: # (::contents:start) A robust Error subclass with error codes, typed debug values, and recursive stack traces using native Error.cause. ## Usage First, require me where you could throw errors: ```js import YError from 'yerror'; ``` Then, emit errors with a bonus: parameters! ```js function doSomething(pay, action) { if (parseInt(pay, 10) !== pay) { throw new YError('E_BAD_PAY', [pay, action]); } } doSomething('nuts', 'code'); // YError: E_BAD_PAY (["nuts", "code"]) // at doSomething (/home/nfroidure/nfroidure/yerror/test.js:5:11) // at Object.<anonymous> (/home/nfroidure/nfroidure/yerror/test.js:9:1) // (...) ``` You don't have to use constant like error messages, we use this convention mainly for i18n reasons. Also, you could want to wrap errors and keep a valuable stack trace: ```js function doSomethingAsync(pay, action) { return new Promise(function (resolve, reject) { try { doSomething(pay, action); resolve(); } catch (err) { reject(YError.bump(err)); } }); } doSomethingAsync('nuts', 'code').catch(function (err) { console.log(err.stack); }); // YError: E_BAD_PAY (nuts, code) // at doSomething (/home/nfroidure/nfroidure/yerror/test.js:5:11) // (...) // Caused by: YError: E_BAD_TRANSACTION (pay) // at Function.YError.wrap (/home/nfroidure/nfroidure/yerror/src/index.js:41:12) // at /home/nfroidure/nfroidure/yerror/test.js:16:21 // at doSomethingAsync (/home/nfroidure/nfroidure/yerror/test.js:11:11) // (...) ``` ## Global Error Registry You can now get full autocompletion and type-safety for your error codes and their debug data. ```ts import { YError } from 'yerror'; declare module 'yerror' { interface YErrorRegistry { E_USER_NOT_FOUND: [userId: string]; } } // TypeScript will now enforce the correct arguments: throw new YError('E_USER_NOT_FOUND', ['123']); // Users of you own code will then be able to cast errors try { getUser('123'); } catch (err) { if (hasYErrorCode(err, 'E_USER_NOT_FOUND')) { console.log(err.debug[0]); } } ``` [//]: # (::contents:end) # API ## Classes <dl> <dt><a href="#YError">YError</a> β <code>Error</code></dt> <dd><p>A YError class able to contain some debug and print better stack traces</p> </dd> </dl> ## Functions <dl> <dt><a href="#printStackTrace">printStackTrace(err)</a> β <code>string</code></dt> <dd><p>Allow to print a stack from anything (especially caught errors that may or may not contain errors π€·).</p> </dd> <dt><a href="#hasYErrorCode">hasYErrorCode(err, code)</a> β <code>boolean</code></dt> <dd><p>Allow to check a YError code and cast the error.</p> </dd> <dt><a href="#pickYErrorWithCode">pickYErrorWithCode(err, code)</a> β <code>boolean</code></dt> <dd><p>Allow to check all errors for a YError code and return the casted the error.</p> </dd> </dl> <a name="YError"></a> ## YError β <code>Error</code> A YError class able to contain some debug and print better stack traces **Kind**: global class **Extends**: <code>Error</code> * [YError](#YError) β <code>Error</code> * [new YError([errorCode], [debug], options)](#new_YError_new) * [.wrap(err, [errorCode], [debug])](#YError.wrap) β [<code>YError</code>](#YError) * [.cast(err, [errorCode], [debug])](#YError.cast) β [<code>YError</code>](#YError) * [.bump(err, [errorCode], [debug])](#YError.bump) β [<code>YError</code>](#YError) <a name="new_YError_new"></a> ### new YError([errorCode], [debug], options) Creates a new YError with an error code and some debug as debug values. | Param | Type | Default | Description | | --- | --- | --- | --- | | [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error | | [debug] | <code>any</code> | | Some additional debugging values The error options | | options | <code>Object</code> | | The error options | <a name="YError.wrap"></a> ### YError.wrap(err, [errorCode], [debug]) β [<code>YError</code>](#YError) Wraps any error and output a YError with an error code and some debug as debug values. **Kind**: static method of [<code>YError</code>](#YError) **Returns**: [<code>YError</code>](#YError) - The wrapped error | Param | Type | Default | Description | | --- | --- | --- | --- | | err | <code>Error</code> | | The error to wrap | | [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error | | [debug] | <code>any</code> | | Some additional debugging values | <a name="YError.cast"></a> ### YError.cast(err, [errorCode], [debug]) β [<code>YError</code>](#YError) Return a YError as is or wraps any other error and output a YError with a code and some debug as debug values. **Kind**: static method of [<code>YError</code>](#YError) **Returns**: [<code>YError</code>](#YError) - The wrapped error | Param | Type | Default | Description | | --- | --- | --- | --- | | err | <code>Error</code> | | The error to cast | | [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error | | [debug] | <code>any</code> | | Some additional debugging values | <a name="YError.bump"></a> ### YError.bump(err, [errorCode], [debug]) β [<code>YError</code>](#YError) Same than `YError.wrap()` but preserves the code and the debug values of the error if it is already an instance of the YError constructor. **Kind**: static method of [<code>YError</code>](#YError) **Returns**: [<code>YError</code>](#YError) - The wrapped error | Param | Type | Default | Description | | --- | --- | --- | --- | | err | <code>Error</code> | | The error to bump | | [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error | | [debug] | <code>any</code> | | Some additional debugging values | <a name="printStackTrace"></a> ## printStackTrace(err) β <code>string</code> Allow to print a stack from anything (especially caught errors that may or may not contain errors π€·). **Kind**: global function **Returns**: <code>string</code> - The stack trace if any | Param | Type | Description | | --- | --- | --- | | err | <code>Error</code> | The error to print | <a name="hasYErrorCode"></a> ## hasYErrorCode(err, code) β <code>boolean</code> Allow to check a YError code and cast the error. **Kind**: global function **Returns**: <code>boolean</code> - The result | Param | Type | Description | | --- | --- | --- | | err | <code>Error</code> | The error to cast | | code | <code>Error</code> | The code to check | <a name="pickYErrorWithCode"></a> ## pickYErrorWithCode(err, code) β <code>boolean</code> Allow to check all errors for a YError code and return the casted the error. **Kind**: global function **Returns**: <code>boolean</code> - The result | Param | Type | Description | | --- | --- | --- | | err | <code>Error</code> | The error to cast | | code | <code>Error</code> | The code to check | # Authors - [Nicolas Froidure (formerly at SimpliField)](http://insertafter.com/en/index.html) # License [MIT](https://github.com/nfroidure/yerror/blob/main/LICENSE)