@nice-code/common-errors 0.0.19 → 0.0.21

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.
@@ -380,6 +380,7 @@ class NiceError extends Error {
380
380
  ids;
381
381
  wasntNice;
382
382
  httpStatusCode;
383
+ timeCreated;
383
384
  originError;
384
385
  _packedState;
385
386
  _errorDataMap;
@@ -393,6 +394,7 @@ class NiceError extends Error {
393
394
  if (options.originError != null) {
394
395
  this.originError = options.originError;
395
396
  }
397
+ this.timeCreated = options.timeCreated ?? Date.now();
396
398
  }
397
399
  hasId(id) {
398
400
  return id in this._errorDataMap;
@@ -470,7 +472,12 @@ class NiceError extends Error {
470
472
  } else {
471
473
  contextState = data.contextState;
472
474
  }
473
- errorData[id] = { contextState, message: data.message, httpStatusCode: data.httpStatusCode };
475
+ errorData[id] = {
476
+ contextState,
477
+ message: data.message,
478
+ httpStatusCode: data.httpStatusCode,
479
+ timeAdded: data.timeAdded
480
+ };
474
481
  }
475
482
  return {
476
483
  name: "NiceError",
@@ -480,34 +487,49 @@ class NiceError extends Error {
480
487
  wasntNice: this.wasntNice,
481
488
  message: this.message,
482
489
  httpStatusCode: this.httpStatusCode,
490
+ timeCreated: this.timeCreated,
483
491
  ...this.stack != null ? { stack: this.stack } : {},
484
492
  originError
485
493
  };
486
494
  }
495
+ toJsonString() {
496
+ return JSON.stringify(this.toJsonObject());
497
+ }
498
+ toHttpResponse() {
499
+ return new Response(this.toJsonString(), {
500
+ status: this.httpStatusCode,
501
+ headers: { "Content-Type": "application/json" }
502
+ });
503
+ }
487
504
  hydrate(definedNiceError) {
488
505
  return definedNiceError.hydrate(this);
489
506
  }
490
- handleWith(cases) {
491
- for (const c of cases) {
492
- if (!c._domain.isExact(this))
493
- continue;
494
- if (c._ids != null && !this.hasOneOfIds(c._ids))
495
- continue;
496
- c._handler(c._domain.hydrate(this));
497
- return true;
507
+ handleWithSync(handlerInput, handlerOptions = {}) {
508
+ const handlersArray = Array.isArray(handlerInput) ? handlerInput : [handlerInput];
509
+ for (const handler of handlersArray) {
510
+ const result = handler.handleErrorWithPromiseInspection(this, handlerOptions);
511
+ if (result.matched) {
512
+ if (result.isPromise) {
513
+ console.warn(`[NiceError.handleWith] Handler ${result.target.identifier} returned a Promise but was called via \`handleWith\` (synchronous). ` + `The Promise will not be awaited. Use \`handleWithAsync\` for async handlers.`);
514
+ }
515
+ return result.isPromise ? undefined : result.handlerResponse;
516
+ }
498
517
  }
499
- return false;
518
+ if (handlerOptions.throwOnUnhandled === true)
519
+ throw this;
520
+ return;
500
521
  }
501
- async handleWithAsync(cases) {
502
- for (const c of cases) {
503
- if (!c._domain.isExact(this))
504
- continue;
505
- if (c._ids != null && !this.hasOneOfIds(c._ids))
506
- continue;
507
- await c._handler(c._domain.hydrate(this));
508
- return true;
522
+ async handleWithAsync(handlerInput, handlerOptions = {}) {
523
+ const handlersArray = Array.isArray(handlerInput) ? handlerInput : [handlerInput];
524
+ for (const handler of handlersArray) {
525
+ const result = handler.handleErrorWithPromiseInspection(this, handlerOptions);
526
+ if (result.matched) {
527
+ return result.isPromise ? await result.handlerPromise : result.handlerResponse;
528
+ }
509
529
  }
510
- return false;
530
+ if (handlerOptions.throwOnUnhandled === true)
531
+ throw this;
532
+ return;
511
533
  }
512
534
  get isPacked() {
513
535
  return this._packedState != null;
@@ -587,7 +609,7 @@ class NiceErrorHydrated extends NiceError {
587
609
  }
588
610
 
589
611
  // ../nice-error/src/NiceErrorDefined/NiceErrorDefined.ts
590
- class NiceErrorDefined {
612
+ class NiceErrorDomain {
591
613
  domain;
592
614
  allDomains;
593
615
  defaultHttpStatusCode;
@@ -612,7 +634,7 @@ class NiceErrorDefined {
612
634
  }
613
635
  }
614
636
  createChildDomain(subErrorDef) {
615
- const child = new NiceErrorDefined({
637
+ const child = new NiceErrorDomain({
616
638
  domain: subErrorDef.domain,
617
639
  allDomains: [subErrorDef.domain, ...this.allDomains],
618
640
  schema: subErrorDef.schema,
@@ -668,9 +690,10 @@ class NiceErrorDefined {
668
690
  if (errDef.domain !== this.domain) {
669
691
  throw new Error(`[NiceErrorDefined.hydrate] Domain mismatch: this definition is "${this.domain}" ` + `but the error belongs to "${errDef.domain}". ` + `Call \`niceErrorDefined.is(error)\` before hydrating to ensure compatibility.`);
670
692
  }
693
+ const finalError = error instanceof NiceError ? error : new NiceError(error);
671
694
  const reconciledErrorData = {};
672
- for (const id of error.getIds()) {
673
- const existingData = error.getErrorDataForId(id);
695
+ for (const id of finalError.getIds()) {
696
+ const existingData = finalError.getErrorDataForId(id);
674
697
  if (existingData == null)
675
698
  continue;
676
699
  let contextState = existingData.contextState;
@@ -688,18 +711,20 @@ class NiceErrorDefined {
688
711
  reconciledErrorData[id] = {
689
712
  contextState,
690
713
  message: existingData.message,
691
- httpStatusCode: existingData.httpStatusCode
714
+ httpStatusCode: existingData.httpStatusCode,
715
+ timeAdded: existingData.timeAdded
692
716
  };
693
717
  }
694
718
  return new NiceErrorHydrated({
695
719
  def: this._buildDef(),
696
720
  niceErrorDefined: this,
697
- ids: error.ids,
721
+ ids: finalError.ids,
698
722
  errorData: reconciledErrorData,
699
- message: error.message,
700
- httpStatusCode: error.httpStatusCode,
701
- wasntNice: error.wasntNice,
702
- originError: error.originError
723
+ message: finalError.message,
724
+ httpStatusCode: finalError.httpStatusCode,
725
+ wasntNice: finalError.wasntNice,
726
+ originError: finalError.originError,
727
+ timeCreated: finalError.timeCreated
703
728
  });
704
729
  }
705
730
  fromId(...args) {
@@ -790,13 +815,13 @@ class NiceErrorDefined {
790
815
  } else {
791
816
  contextState = { kind: "serde_unset" /* serde_unset */, value: context };
792
817
  }
793
- return { contextState, message, httpStatusCode };
818
+ return { contextState, message, httpStatusCode, timeAdded: Date.now() };
794
819
  }
795
820
  }
796
821
 
797
822
  // ../nice-error/src/NiceErrorDefined/defineNiceError.ts
798
823
  var defineNiceError = (definition) => {
799
- return new NiceErrorDefined({
824
+ return new NiceErrorDomain({
800
825
  domain: definition.domain,
801
826
  allDomains: [definition.domain],
802
827
  schema: definition.schema,
@@ -864,6 +889,10 @@ var err_cast_not_nice = err_nice.createChildDomain({
864
889
  })
865
890
  }
866
891
  });
892
+ var err_nice_handler = err_nice.createChildDomain({
893
+ domain: "err_nice_handler",
894
+ schema: {}
895
+ });
867
896
  // ../../node_modules/.bun/tslog@4.10.2/node_modules/tslog/esm/urlToObj.js
868
897
  function urlToObject(url) {
869
898
  return {
package/build/index.js CHANGED
@@ -380,6 +380,7 @@ class NiceError extends Error {
380
380
  ids;
381
381
  wasntNice;
382
382
  httpStatusCode;
383
+ timeCreated;
383
384
  originError;
384
385
  _packedState;
385
386
  _errorDataMap;
@@ -393,6 +394,7 @@ class NiceError extends Error {
393
394
  if (options.originError != null) {
394
395
  this.originError = options.originError;
395
396
  }
397
+ this.timeCreated = options.timeCreated ?? Date.now();
396
398
  }
397
399
  hasId(id) {
398
400
  return id in this._errorDataMap;
@@ -470,7 +472,12 @@ class NiceError extends Error {
470
472
  } else {
471
473
  contextState = data.contextState;
472
474
  }
473
- errorData[id] = { contextState, message: data.message, httpStatusCode: data.httpStatusCode };
475
+ errorData[id] = {
476
+ contextState,
477
+ message: data.message,
478
+ httpStatusCode: data.httpStatusCode,
479
+ timeAdded: data.timeAdded
480
+ };
474
481
  }
475
482
  return {
476
483
  name: "NiceError",
@@ -480,34 +487,49 @@ class NiceError extends Error {
480
487
  wasntNice: this.wasntNice,
481
488
  message: this.message,
482
489
  httpStatusCode: this.httpStatusCode,
490
+ timeCreated: this.timeCreated,
483
491
  ...this.stack != null ? { stack: this.stack } : {},
484
492
  originError
485
493
  };
486
494
  }
495
+ toJsonString() {
496
+ return JSON.stringify(this.toJsonObject());
497
+ }
498
+ toHttpResponse() {
499
+ return new Response(this.toJsonString(), {
500
+ status: this.httpStatusCode,
501
+ headers: { "Content-Type": "application/json" }
502
+ });
503
+ }
487
504
  hydrate(definedNiceError) {
488
505
  return definedNiceError.hydrate(this);
489
506
  }
490
- handleWith(cases) {
491
- for (const c of cases) {
492
- if (!c._domain.isExact(this))
493
- continue;
494
- if (c._ids != null && !this.hasOneOfIds(c._ids))
495
- continue;
496
- c._handler(c._domain.hydrate(this));
497
- return true;
507
+ handleWithSync(handlerInput, handlerOptions = {}) {
508
+ const handlersArray = Array.isArray(handlerInput) ? handlerInput : [handlerInput];
509
+ for (const handler of handlersArray) {
510
+ const result = handler.handleErrorWithPromiseInspection(this, handlerOptions);
511
+ if (result.matched) {
512
+ if (result.isPromise) {
513
+ console.warn(`[NiceError.handleWith] Handler ${result.target.identifier} returned a Promise but was called via \`handleWith\` (synchronous). ` + `The Promise will not be awaited. Use \`handleWithAsync\` for async handlers.`);
514
+ }
515
+ return result.isPromise ? undefined : result.handlerResponse;
516
+ }
498
517
  }
499
- return false;
518
+ if (handlerOptions.throwOnUnhandled === true)
519
+ throw this;
520
+ return;
500
521
  }
501
- async handleWithAsync(cases) {
502
- for (const c of cases) {
503
- if (!c._domain.isExact(this))
504
- continue;
505
- if (c._ids != null && !this.hasOneOfIds(c._ids))
506
- continue;
507
- await c._handler(c._domain.hydrate(this));
508
- return true;
522
+ async handleWithAsync(handlerInput, handlerOptions = {}) {
523
+ const handlersArray = Array.isArray(handlerInput) ? handlerInput : [handlerInput];
524
+ for (const handler of handlersArray) {
525
+ const result = handler.handleErrorWithPromiseInspection(this, handlerOptions);
526
+ if (result.matched) {
527
+ return result.isPromise ? await result.handlerPromise : result.handlerResponse;
528
+ }
509
529
  }
510
- return false;
530
+ if (handlerOptions.throwOnUnhandled === true)
531
+ throw this;
532
+ return;
511
533
  }
512
534
  get isPacked() {
513
535
  return this._packedState != null;
@@ -587,7 +609,7 @@ class NiceErrorHydrated extends NiceError {
587
609
  }
588
610
 
589
611
  // ../nice-error/src/NiceErrorDefined/NiceErrorDefined.ts
590
- class NiceErrorDefined {
612
+ class NiceErrorDomain {
591
613
  domain;
592
614
  allDomains;
593
615
  defaultHttpStatusCode;
@@ -612,7 +634,7 @@ class NiceErrorDefined {
612
634
  }
613
635
  }
614
636
  createChildDomain(subErrorDef) {
615
- const child = new NiceErrorDefined({
637
+ const child = new NiceErrorDomain({
616
638
  domain: subErrorDef.domain,
617
639
  allDomains: [subErrorDef.domain, ...this.allDomains],
618
640
  schema: subErrorDef.schema,
@@ -668,9 +690,10 @@ class NiceErrorDefined {
668
690
  if (errDef.domain !== this.domain) {
669
691
  throw new Error(`[NiceErrorDefined.hydrate] Domain mismatch: this definition is "${this.domain}" ` + `but the error belongs to "${errDef.domain}". ` + `Call \`niceErrorDefined.is(error)\` before hydrating to ensure compatibility.`);
670
692
  }
693
+ const finalError = error instanceof NiceError ? error : new NiceError(error);
671
694
  const reconciledErrorData = {};
672
- for (const id of error.getIds()) {
673
- const existingData = error.getErrorDataForId(id);
695
+ for (const id of finalError.getIds()) {
696
+ const existingData = finalError.getErrorDataForId(id);
674
697
  if (existingData == null)
675
698
  continue;
676
699
  let contextState = existingData.contextState;
@@ -688,18 +711,20 @@ class NiceErrorDefined {
688
711
  reconciledErrorData[id] = {
689
712
  contextState,
690
713
  message: existingData.message,
691
- httpStatusCode: existingData.httpStatusCode
714
+ httpStatusCode: existingData.httpStatusCode,
715
+ timeAdded: existingData.timeAdded
692
716
  };
693
717
  }
694
718
  return new NiceErrorHydrated({
695
719
  def: this._buildDef(),
696
720
  niceErrorDefined: this,
697
- ids: error.ids,
721
+ ids: finalError.ids,
698
722
  errorData: reconciledErrorData,
699
- message: error.message,
700
- httpStatusCode: error.httpStatusCode,
701
- wasntNice: error.wasntNice,
702
- originError: error.originError
723
+ message: finalError.message,
724
+ httpStatusCode: finalError.httpStatusCode,
725
+ wasntNice: finalError.wasntNice,
726
+ originError: finalError.originError,
727
+ timeCreated: finalError.timeCreated
703
728
  });
704
729
  }
705
730
  fromId(...args) {
@@ -790,13 +815,13 @@ class NiceErrorDefined {
790
815
  } else {
791
816
  contextState = { kind: "serde_unset" /* serde_unset */, value: context };
792
817
  }
793
- return { contextState, message, httpStatusCode };
818
+ return { contextState, message, httpStatusCode, timeAdded: Date.now() };
794
819
  }
795
820
  }
796
821
 
797
822
  // ../nice-error/src/NiceErrorDefined/defineNiceError.ts
798
823
  var defineNiceError = (definition) => {
799
- return new NiceErrorDefined({
824
+ return new NiceErrorDomain({
800
825
  domain: definition.domain,
801
826
  allDomains: [definition.domain],
802
827
  schema: definition.schema,
@@ -864,6 +889,10 @@ var err_cast_not_nice = err_nice.createChildDomain({
864
889
  })
865
890
  }
866
891
  });
892
+ var err_nice_handler = err_nice.createChildDomain({
893
+ domain: "err_nice_handler",
894
+ schema: {}
895
+ });
867
896
  // ../../node_modules/.bun/tslog@4.10.2/node_modules/tslog/esm/urlToObj.js
868
897
  function urlToObject(url) {
869
898
  return {
@@ -2,7 +2,7 @@ import type { IErrContext_HonoStandardSchema } from "./err_validation.types";
2
2
  export declare enum EValidator {
3
3
  standard_schema = "standard_schema"
4
4
  }
5
- export declare const err_validation: import("@nice-code/error").NiceErrorDefined<{
5
+ export declare const err_validation: import("@nice-code/error").NiceErrorDomain<{
6
6
  domain: string;
7
7
  allDomains: [string, "err_nice"];
8
8
  schema: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nice-code/common-errors",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -29,7 +29,7 @@
29
29
  "build-types": "tsc --project tsconfig.build.json"
30
30
  },
31
31
  "dependencies": {
32
- "@nice-code/error": "0.0.14",
32
+ "@nice-code/error": "0.0.21",
33
33
  "@standard-schema/spec": "^1.1.0",
34
34
  "@hono/standard-validator": "^0.2.2",
35
35
  "http-status-codes": "^2.3.0"