index.d.ts 2.8 KB

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