kerium 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/error.d.ts CHANGED
@@ -402,39 +402,56 @@ declare const errnoMessages: {
402
402
  };
403
403
  type ErrnoMessage<T extends Errno | keyof typeof Errno> = (typeof errnoMessages)[T extends Errno ? T : T extends keyof typeof Errno ? (typeof Errno)[T] : ''];
404
404
  export declare function strerror<const T extends Errno | keyof typeof Errno>(errno: T): ErrnoMessage<T>;
405
+ export interface ExceptionExtra {
406
+ path?: string;
407
+ dest?: string;
408
+ syscall?: string;
409
+ [k: string]: any;
410
+ }
405
411
  /**
406
412
  * JSON representation of an error.
407
413
  */
408
- export interface ErrnoExceptionJSON {
414
+ export interface ExceptionJSON {
409
415
  errno: Errno;
410
416
  message: string;
411
- path?: string;
412
417
  code: keyof typeof Errno;
413
418
  stack: string;
414
- syscall: string;
419
+ path?: string;
420
+ dest?: string;
421
+ syscall?: string;
415
422
  }
416
423
  /**
417
424
  * An error with additional information about what happened.
418
425
  *
419
- * @remarks
426
+ * @privateRemarks
427
+ *
428
+ * This is modeled after Node.js's `ErrnoException` and `UVException`.
420
429
  *
421
430
  * `Error.captureStackTrace` is used when available to hide irrelevant stack frames.
422
431
  * This is being standardized, however it is not available in Deno and behind a flag in Firefox.
423
432
  * See https://github.com/tc39/proposal-error-capturestacktrace for more details.
424
433
  */
425
- export declare class ErrnoException extends Error implements NodeJS.ErrnoException {
434
+ export declare class Exception extends Error implements NodeJS.ErrnoException, ExceptionJSON {
426
435
  errno: Errno;
427
- message: string;
428
- syscall: string;
429
436
  stack: string;
430
437
  code: keyof typeof Errno;
431
- constructor(errno: Errno, message?: string, syscall?: string);
438
+ path?: string;
439
+ dest?: string;
440
+ syscall?: string;
441
+ constructor(errno: Errno, message: false, context: ExceptionExtra);
442
+ constructor(errno: Errno, message?: string);
432
443
  toString(): string;
433
- toJSON(): ErrnoExceptionJSON;
434
- static fromJSON(this: void, json: ErrnoExceptionJSON): ErrnoException;
444
+ toJSON(): ExceptionJSON;
445
+ static fromJSON(this: void, json: ExceptionJSON): Exception;
435
446
  }
447
+ /**
448
+ * Shortcut for UV-style exceptions.
449
+ */
450
+ export declare function UV(this: void, code: keyof typeof Errno, syscall: string, path?: string, dest?: string): Exception;
451
+ export declare function UV(this: void, code: keyof typeof Errno, context?: ExceptionExtra): Exception;
436
452
  /**
437
453
  * Shortcut to easily create an `ErrnoException` with a specific error code.
438
454
  */
439
- export declare function withErrno(this: void, code: keyof typeof Errno, syscall?: string, message?: string): ErrnoException;
455
+ export declare function withErrno(this: void, code: keyof typeof Errno, message?: string): Exception;
456
+ export declare function rethrow(extra: ExceptionExtra): (e: Exception) => never;
440
457
  export {};
package/dist/error.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { omit } from 'utilium';
1
2
  /**
2
3
  * Standard POSIX error codes.
3
4
  * @see https://en.wikipedia.org/wiki/Errno.h
@@ -405,52 +406,91 @@ export function strerror(errno) {
405
406
  const _errno = typeof errno == 'string' ? Errno[errno] : errno;
406
407
  return (_errno in errnoMessages ? errnoMessages[_errno] : '');
407
408
  }
409
+ function setUVMessage(ex) {
410
+ let message = `${ex.code}: ${errnoMessages[ex.errno]}, ${ex.syscall}`;
411
+ if (ex.path)
412
+ message += ` '${ex.path}'`;
413
+ if (ex.dest)
414
+ message += ` -> '${ex.dest}'`;
415
+ if (ex.message)
416
+ message += ` (${ex.message})`;
417
+ ex.message = message;
418
+ }
408
419
  /**
409
420
  * An error with additional information about what happened.
410
421
  *
411
- * @remarks
422
+ * @privateRemarks
423
+ *
424
+ * This is modeled after Node.js's `ErrnoException` and `UVException`.
412
425
  *
413
426
  * `Error.captureStackTrace` is used when available to hide irrelevant stack frames.
414
427
  * This is being standardized, however it is not available in Deno and behind a flag in Firefox.
415
428
  * See https://github.com/tc39/proposal-error-capturestacktrace for more details.
416
429
  */
417
- export class ErrnoException extends Error {
430
+ export class Exception extends Error {
418
431
  errno;
419
- message;
420
- syscall;
421
432
  code;
422
- constructor(errno, message = errnoMessages[errno], syscall = '') {
433
+ path;
434
+ dest;
435
+ syscall;
436
+ constructor(errno, message, ctx = {}) {
437
+ const code = Errno[errno];
438
+ if (message === false) {
439
+ message = `${code}: ${errnoMessages[errno]}, ${ctx.syscall}`;
440
+ if (ctx.path)
441
+ message += ` '${ctx.path}'`;
442
+ if (ctx.dest)
443
+ message += ` -> '${ctx.dest}'`;
444
+ }
423
445
  super(message);
424
446
  this.errno = errno;
425
- this.message = message;
426
- this.syscall = syscall;
427
- this.code = Errno[errno];
447
+ this.code = code;
448
+ Object.assign(this, omit(ctx, 'message', 'path', 'dest'));
428
449
  Error.captureStackTrace?.(this, this.constructor);
429
450
  }
430
451
  toString() {
431
- return this.code + ': ' + this.message;
452
+ return this.message;
432
453
  }
433
454
  toJSON() {
434
- return {
455
+ const json = {
435
456
  errno: this.errno,
436
457
  code: this.code,
437
458
  stack: this.stack,
438
459
  message: this.message,
439
- syscall: this.syscall,
440
460
  };
461
+ if (this.path)
462
+ json.path = this.path;
463
+ if (this.dest)
464
+ json.dest = this.dest;
465
+ if (this.syscall)
466
+ json.syscall = this.syscall;
467
+ return json;
441
468
  }
442
469
  static fromJSON(json) {
443
- const err = new ErrnoException(json.errno, json.message, json.syscall);
444
- err.code = json.code;
470
+ const err = json.syscall ? new Exception(json.errno, false, json) : new Exception(json.errno, json.message);
445
471
  err.stack = json.stack;
446
472
  return err;
447
473
  }
448
474
  }
475
+ export function UV(code, context, path, dest) {
476
+ if (typeof context === 'string')
477
+ context = { syscall: context, path, dest };
478
+ const err = new Exception(Errno[code], false, context ?? {});
479
+ Error.captureStackTrace?.(err, UV);
480
+ return err;
481
+ }
449
482
  /**
450
483
  * Shortcut to easily create an `ErrnoException` with a specific error code.
451
484
  */
452
- export function withErrno(code, syscall, message) {
453
- const err = new ErrnoException(Errno[code], message ?? errnoMessages[Errno[code]], syscall);
485
+ export function withErrno(code, message) {
486
+ const err = new Exception(Errno[code], message ?? errnoMessages[Errno[code]]);
454
487
  Error.captureStackTrace?.(err, withErrno);
455
488
  return err;
456
489
  }
490
+ export function rethrow(extra) {
491
+ return function (e) {
492
+ Object.assign(e, extra);
493
+ setUVMessage(e);
494
+ throw e;
495
+ };
496
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kerium",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "POSIX-style errors, logging, and more",
5
5
  "author": "James Prevett <jp@jamespre.dev> (https://jamespre.dev)",
6
6
  "repository": {