kerium 1.2.1 → 1.3.1

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,60 @@ 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
  }
423
+ /**
424
+ * Set the message of an exception to the UV-style message.
425
+ */
426
+ export declare function setUVMessage<T extends ExceptionJSON>(ex: T): T;
416
427
  /**
417
428
  * An error with additional information about what happened.
418
429
  *
419
- * @remarks
430
+ * @privateRemarks
431
+ *
432
+ * This is modeled after Node.js's `ErrnoException` and `UVException`.
420
433
  *
421
434
  * `Error.captureStackTrace` is used when available to hide irrelevant stack frames.
422
435
  * This is being standardized, however it is not available in Deno and behind a flag in Firefox.
423
436
  * See https://github.com/tc39/proposal-error-capturestacktrace for more details.
424
437
  */
425
- export declare class ErrnoException extends Error implements NodeJS.ErrnoException {
438
+ export declare class Exception extends Error implements NodeJS.ErrnoException, ExceptionJSON {
426
439
  errno: Errno;
427
- message: string;
428
- syscall: string;
429
440
  stack: string;
430
441
  code: keyof typeof Errno;
431
- constructor(errno: Errno, message?: string, syscall?: string);
442
+ path?: string;
443
+ dest?: string;
444
+ syscall?: string;
445
+ constructor(errno: Errno, message: false, context: ExceptionExtra);
446
+ constructor(errno: Errno, message?: string);
432
447
  toString(): string;
433
- toJSON(): ErrnoExceptionJSON;
434
- static fromJSON(this: void, json: ErrnoExceptionJSON): ErrnoException;
448
+ toJSON(): ExceptionJSON;
449
+ static fromJSON(this: void, json: ExceptionJSON): Exception;
435
450
  }
451
+ /**
452
+ * Shortcut for UV-style exceptions.
453
+ */
454
+ export declare function UV(this: void, code: keyof typeof Errno, syscall: string, path?: string, dest?: string): Exception;
455
+ export declare function UV(this: void, code: keyof typeof Errno, context?: ExceptionExtra): Exception;
436
456
  /**
437
457
  * Shortcut to easily create an `ErrnoException` with a specific error code.
438
458
  */
439
- export declare function withErrno(this: void, code: keyof typeof Errno, message?: string, syscall?: string): ErrnoException;
459
+ export declare function withErrno(this: void, code: keyof typeof Errno, message?: string): Exception;
460
+ export declare function rethrow(extra: ExceptionExtra): (e: Exception) => never;
440
461
  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,95 @@ 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
+ /**
410
+ * Set the message of an exception to the UV-style message.
411
+ */
412
+ export function setUVMessage(ex) {
413
+ let message = `${ex.code}: ${errnoMessages[ex.errno]}, ${ex.syscall}`;
414
+ if (ex.path)
415
+ message += ` '${ex.path}'`;
416
+ if (ex.dest)
417
+ message += ` -> '${ex.dest}'`;
418
+ if (ex.message)
419
+ message += ` (${ex.message})`;
420
+ ex.message = message;
421
+ return ex;
422
+ }
408
423
  /**
409
424
  * An error with additional information about what happened.
410
425
  *
411
- * @remarks
426
+ * @privateRemarks
427
+ *
428
+ * This is modeled after Node.js's `ErrnoException` and `UVException`.
412
429
  *
413
430
  * `Error.captureStackTrace` is used when available to hide irrelevant stack frames.
414
431
  * This is being standardized, however it is not available in Deno and behind a flag in Firefox.
415
432
  * See https://github.com/tc39/proposal-error-capturestacktrace for more details.
416
433
  */
417
- export class ErrnoException extends Error {
434
+ export class Exception extends Error {
418
435
  errno;
419
- message;
420
- syscall;
421
436
  code;
422
- constructor(errno, message = errnoMessages[errno], syscall = '') {
437
+ path;
438
+ dest;
439
+ syscall;
440
+ constructor(errno, message, ctx = {}) {
441
+ const code = Errno[errno];
442
+ if (message === false) {
443
+ message = `${code}: ${errnoMessages[errno]}, ${ctx.syscall}`;
444
+ if (ctx.path)
445
+ message += ` '${ctx.path}'`;
446
+ if (ctx.dest)
447
+ message += ` -> '${ctx.dest}'`;
448
+ }
423
449
  super(message);
424
450
  this.errno = errno;
425
- this.message = message;
426
- this.syscall = syscall;
427
- this.code = Errno[errno];
451
+ this.code = code;
452
+ Object.assign(this, omit(ctx, 'message', 'path', 'dest'));
428
453
  Error.captureStackTrace?.(this, this.constructor);
429
454
  }
430
455
  toString() {
431
- return this.code + ': ' + this.message;
456
+ return this.message;
432
457
  }
433
458
  toJSON() {
434
- return {
459
+ const json = {
435
460
  errno: this.errno,
436
461
  code: this.code,
437
462
  stack: this.stack,
438
463
  message: this.message,
439
- syscall: this.syscall,
440
464
  };
465
+ if (this.path)
466
+ json.path = this.path;
467
+ if (this.dest)
468
+ json.dest = this.dest;
469
+ if (this.syscall)
470
+ json.syscall = this.syscall;
471
+ return json;
441
472
  }
442
473
  static fromJSON(json) {
443
- const err = new ErrnoException(json.errno, json.message, json.syscall);
444
- err.code = json.code;
474
+ const err = json.syscall ? new Exception(json.errno, false, json) : new Exception(json.errno, json.message);
445
475
  err.stack = json.stack;
446
476
  return err;
447
477
  }
448
478
  }
479
+ export function UV(code, context, path, dest) {
480
+ if (typeof context === 'string')
481
+ context = { syscall: context, path, dest };
482
+ const err = new Exception(Errno[code], false, context ?? {});
483
+ Error.captureStackTrace?.(err, UV);
484
+ return err;
485
+ }
449
486
  /**
450
487
  * Shortcut to easily create an `ErrnoException` with a specific error code.
451
488
  */
452
- export function withErrno(code, message, syscall) {
453
- const err = new ErrnoException(Errno[code], message ?? errnoMessages[Errno[code]], syscall);
489
+ export function withErrno(code, message) {
490
+ const err = new Exception(Errno[code], message ?? errnoMessages[Errno[code]]);
454
491
  Error.captureStackTrace?.(err, withErrno);
455
492
  return err;
456
493
  }
494
+ export function rethrow(extra) {
495
+ return function (e) {
496
+ Object.assign(e, extra);
497
+ setUVMessage(e);
498
+ throw e;
499
+ };
500
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kerium",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "description": "POSIX-style errors, logging, and more",
5
5
  "author": "James Prevett <jp@jamespre.dev> (https://jamespre.dev)",
6
6
  "repository": {