index.d.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. export = OError;
  2. /**
  3. * Light-weight helpers for handling JavaScript Errors in node.js and the
  4. * browser.
  5. */
  6. declare class OError extends Error {
  7. /**
  8. * Tag debugging information onto any error (whether an OError or not) and
  9. * return it.
  10. *
  11. * @example <caption>An error in a callback</caption>
  12. * function findUser(name, callback) {
  13. * fs.readFile('/etc/passwd', (err, data) => {
  14. * if (err) return callback(OError.tag(err, 'failed to read passwd'))
  15. * // ...
  16. * })
  17. * }
  18. *
  19. * @example <caption>A possible error in a callback</caption>
  20. * function cleanup(callback) {
  21. * fs.unlink('/tmp/scratch', (err) => callback(err && OError.tag(err)))
  22. * }
  23. *
  24. * @example <caption>An error with async/await</caption>
  25. * async function cleanup() {
  26. * try {
  27. * await fs.promises.unlink('/tmp/scratch')
  28. * } catch (err) {
  29. * throw OError.tag(err, 'failed to remove scratch file')
  30. * }
  31. * }
  32. *
  33. * @param {Error} error the error to tag
  34. * @param {string} [message] message with which to tag `error`
  35. * @param {Object} [info] extra data with wich to tag `error`
  36. * @return {Error} the modified `error` argument
  37. */
  38. static tag(error: Error, message?: string, info?: any): Error;
  39. /**
  40. * The merged info from any `tag`s on the given error.
  41. *
  42. * If an info property is repeated, the last one wins.
  43. *
  44. * @param {Error | null | undefined} error any errror (may or may not be an `OError`)
  45. * @return {Object}
  46. */
  47. static getFullInfo(error: Error): any;
  48. /**
  49. * Return the `stack` property from `error`, including the `stack`s for any
  50. * tagged errors added with `OError.tag` and for any `cause`s.
  51. *
  52. * @param {Error | null | undefined} error any error (may or may not be an `OError`)
  53. * @return {string}
  54. */
  55. static getFullStack(error: Error): string;
  56. /**
  57. * @param {string} message as for built-in Error
  58. * @param {Object} [info] extra data to attach to the error
  59. * @param {Error} [cause] the internal error that caused this error
  60. */
  61. constructor(message: string, info?: any, cause?: Error);
  62. info: any;
  63. cause: Error;
  64. /** @private @type {Array<TaggedError> | undefined} */
  65. private _oErrorTags;
  66. /**
  67. * Set the extra info object for this error.
  68. *
  69. * @param {Object} info extra data to attach to the error
  70. * @return {this}
  71. */
  72. withInfo(info: any): OError;
  73. /**
  74. * Wrap the given error, which caused this error.
  75. *
  76. * @param {Error} cause the internal error that caused this error
  77. * @return {this}
  78. */
  79. withCause(cause: Error): OError;
  80. }