From fd6ea2bb70784534d9a54a4fc9b1796c2b4eef1f Mon Sep 17 00:00:00 2001 From: John Lees-Miller Date: Fri, 15 May 2020 11:24:14 +0100 Subject: [PATCH] Add typescript types --- libraries/o-error/index.d.ts | 58 ++++++++++++++++++++++++++++++++++ libraries/o-error/package.json | 6 +++- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 libraries/o-error/index.d.ts diff --git a/libraries/o-error/index.d.ts b/libraries/o-error/index.d.ts new file mode 100644 index 0000000000..000448e49f --- /dev/null +++ b/libraries/o-error/index.d.ts @@ -0,0 +1,58 @@ +export = OError; +/** + * Light-weight helpers for handling JavaScript Errors in node.js and the + * browser. + */ +declare class OError extends Error { + /** + * Tag debugging information onto any error (whether an OError or not) and + * return it. + * + * @param {Error} error the error to tag + * @param {string} [message] message with which to tag `error` + * @param {Object} [info] extra data with wich to tag `error` + * @return {Error} the modified `error` argument + */ + static tag(error: Error, message?: string, info?: any): Error; + /** + * The merged info from any `tag`s on the given error. + * + * If an info property is repeated, the last one wins. + * + * @param {Error | null | undefined} error any errror (may or may not be an `OError`) + * @return {Object} + */ + static getFullInfo(error: Error): any; + /** + * Return the `stack` property from `error`, including the `stack`s for any + * tagged errors added with `OError.tag` and for any `cause`s. + * + * @param {Error | null | undefined} error any error (may or may not be an `OError`) + * @return {string} + */ + static getFullStack(error: Error): string; + /** + * @param {string} message as for built-in Error + * @param {Object} [info] extra data to attach to the error + * @param {Error} [cause] the internal error that caused this error + */ + constructor(message: string, info?: any, cause?: Error); + info: any; + cause: Error; + /** @private @type {Array} */ + private _oErrorTags; + /** + * Set the extra info object for this error. + * + * @param {Object | null | undefined} info extra data to attach to the error + * @return {this} + */ + withInfo(info: any): OError; + /** + * Wrap the given error, which caused this error. + * + * @param {Error} cause the internal error that caused this error + * @return {this} + */ + withCause(cause: Error): OError; +} diff --git a/libraries/o-error/package.json b/libraries/o-error/package.json index 428e6e8731..fbf4351849 100644 --- a/libraries/o-error/package.json +++ b/libraries/o-error/package.json @@ -3,11 +3,15 @@ "version": "2.1.0", "description": "Make custom error types that pass `instanceof` checks, have stack traces, support custom messages and properties, and can wrap causes (like VError).", "main": "index.js", + "types": "index.d.ts", "scripts": { "lint": "eslint .", "update-readme": "doc/update-readme.js", "test": "mocha", - "typecheck": "tsc --allowJs --checkJs --noEmit --moduleResolution node --target ES6 *.js test/**/*.js" + "typecheck": "tsc --allowJs --checkJs --noEmit --moduleResolution node --target ES6 *.js test/**/*.js", + "declaration:build": "rm -f index.d.ts && tsc --allowJs --declaration --emitDeclarationOnly --moduleResolution node --target ES6 index.js", + "declaration:check": "git diff --exit-code -- index.d.ts", + "prepublishOnly": "npm run --silent declaration:build && npm run --silent declaration:check" }, "author": "Overleaf (https://www.overleaf.com)", "license": "MIT",