From fd150ec8405c5c2a0b11fa3223161d18fa83296b Mon Sep 17 00:00:00 2001 From: Sven Reglitzki Date: Tue, 29 Dec 2015 09:47:27 +0100 Subject: [PATCH] Add verror definitions and tests --- verror/verror-tests.ts | 18 ++++++++++++ verror/verror.d.ts | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 verror/verror-tests.ts create mode 100644 verror/verror.d.ts diff --git a/verror/verror-tests.ts b/verror/verror-tests.ts new file mode 100644 index 000000000..22957fdea --- /dev/null +++ b/verror/verror-tests.ts @@ -0,0 +1,18 @@ +// Type definitions for verror v1.6.0 +// Project: https://github.com/davepacheco/node-verror +// Definitions by: Sven Reglitzki +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +import VError = require("verror"); + +var error = new Error("foo"); +var verror1 = new VError(error, "bar"); +var verror2 = new VError.VError(error, "bar"); +var serror = new VError.SError(error, "bar"); +var multiError = new VError.MultiError([verror1, verror2]); +var werror = new VError.WError(verror1, "foobar"); + +var cause1: Error = verror1.cause(); +var cause2: Error = werror.cause(); diff --git a/verror/verror.d.ts b/verror/verror.d.ts new file mode 100644 index 000000000..3ad450776 --- /dev/null +++ b/verror/verror.d.ts @@ -0,0 +1,63 @@ +// Type definitions for verror v1.6.0 +// Project: https://github.com/davepacheco/node-verror +// Definitions by: Sven Reglitzki +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "verror" { + + /* + * VError([cause], fmt[, arg...]): Like JavaScript's built-in Error class, but + * supports a "cause" argument (another error) and a printf-style message. The + * cause argument can be null or omitted entirely. + * + * Examples: + * + * CODE MESSAGE + * new VError('something bad happened') "something bad happened" + * new VError('missing file: "%s"', file) "missing file: "/etc/passwd" + * with file = '/etc/passwd' + * new VError(err, 'open failed') "open failed: file not found" + * with err.message = 'file not found' + */ + class VError extends Error { + static VError: typeof VError; + static SError: typeof SError; + static MultiError: typeof MultiError; + static WError: typeof WError; + cause():Error; + constructor(cause: Error, message: string, ...params: any[]); + constructor(message: string, ...params: any[]); + } + + /* + * SError is like VError, but stricter about types. You cannot pass "null" or + * "undefined" as string arguments to the formatter. Since SError is only a + * different function, not really a different class, we don't set + * SError.prototype.name. + */ + class SError extends VError { + } + + /* + * Represents a collection of errors for the purpose of consumers that generally + * only deal with one error. Callers can extract the individual errors + * contained in this object, but may also just treat it as a normal single + * error, in which case a summary message will be printed. + */ + class MultiError extends VError { + constructor(errors: Error[]); + } + + /* + * Like JavaScript's built-in Error class, but supports a "cause" argument which + * is wrapped, not "folded in" as with VError. Accepts a printf-style message. + * The cause argument can be null. + */ + class WError extends Error { + cause():Error; + constructor(cause: Error, message: string, ...params: any[]); + constructor(message: string, ...params: any[]); + } + + export = VError; +}