@nice-code/common-errors 0.0.20 → 0.1.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/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @nice-code/common-errors
2
+
3
+ Shared error domains for Standard Schema validation errors and Hono middleware integration.
4
+
5
+ See the [main README](../../README.md#nice-codecommon-errors) for full documentation and examples.
@@ -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 {
@@ -2565,7 +2594,7 @@ var niceCatchSValidation = () => async (ctx, next) => {
2565
2594
  }
2566
2595
  }
2567
2596
  };
2568
- // ../../node_modules/.bun/hono@4.12.14/node_modules/hono/dist/utils/url.js
2597
+ // ../../node_modules/.bun/hono@4.12.15/node_modules/hono/dist/utils/url.js
2569
2598
  var tryDecode = (str, decoder) => {
2570
2599
  try {
2571
2600
  return decoder(str);
@@ -2581,7 +2610,7 @@ var tryDecode = (str, decoder) => {
2581
2610
  };
2582
2611
  var decodeURIComponent_ = decodeURIComponent;
2583
2612
 
2584
- // ../../node_modules/.bun/hono@4.12.14/node_modules/hono/dist/utils/cookie.js
2613
+ // ../../node_modules/.bun/hono@4.12.15/node_modules/hono/dist/utils/cookie.js
2585
2614
  var validCookieNameRegEx = /^[\w!#$%&'*.^`|~+-]+$/;
2586
2615
  var validCookieValueRegEx = /^[ !#-:<-[\]-~]*$/;
2587
2616
  var trimCookieWhitespace = (value) => {
@@ -2632,7 +2661,7 @@ var parse = (cookie, name) => {
2632
2661
  return parsedCookie;
2633
2662
  };
2634
2663
 
2635
- // ../../node_modules/.bun/hono@4.12.14/node_modules/hono/dist/helper/cookie/index.js
2664
+ // ../../node_modules/.bun/hono@4.12.15/node_modules/hono/dist/helper/cookie/index.js
2636
2665
  var getCookie = (c, key, prefix) => {
2637
2666
  const cookie = c.req.raw.headers.get("Cookie");
2638
2667
  if (typeof key === "string") {
@@ -2655,7 +2684,7 @@ var getCookie = (c, key, prefix) => {
2655
2684
  return obj;
2656
2685
  };
2657
2686
 
2658
- // ../../node_modules/.bun/hono@4.12.14/node_modules/hono/dist/http-exception.js
2687
+ // ../../node_modules/.bun/hono@4.12.15/node_modules/hono/dist/http-exception.js
2659
2688
  var HTTPException = class extends Error {
2660
2689
  res;
2661
2690
  status;
@@ -2678,7 +2707,7 @@ var HTTPException = class extends Error {
2678
2707
  }
2679
2708
  };
2680
2709
 
2681
- // ../../node_modules/.bun/hono@4.12.14/node_modules/hono/dist/utils/buffer.js
2710
+ // ../../node_modules/.bun/hono@4.12.15/node_modules/hono/dist/utils/buffer.js
2682
2711
  var bufferToFormData = (arrayBuffer, contentType) => {
2683
2712
  const response = new Response(arrayBuffer, {
2684
2713
  headers: {
@@ -2688,7 +2717,7 @@ var bufferToFormData = (arrayBuffer, contentType) => {
2688
2717
  return response.formData();
2689
2718
  };
2690
2719
 
2691
- // ../../node_modules/.bun/hono@4.12.14/node_modules/hono/dist/validator/validator.js
2720
+ // ../../node_modules/.bun/hono@4.12.15/node_modules/hono/dist/validator/validator.js
2692
2721
  var jsonRegex = /^application\/([a-z-\.]+\+)?json(;\s*[a-zA-Z0-9\-]+\=([^;]+))*$/;
2693
2722
  var multipartRegex = /^multipart\/form-data(;\s?boundary=[a-zA-Z0-9'"()+_,\-./:=?]+)?$/;
2694
2723
  var urlencodedRegex = /^application\/x-www-form-urlencoded(;\s*[a-zA-Z0-9\-]+\=([^;]+))*$/;
@@ -2765,7 +2794,7 @@ var validator = (target, validationFunc) => {
2765
2794
  };
2766
2795
  };
2767
2796
 
2768
- // ../../node_modules/.bun/@hono+standard-validator@0.2.2+6f986fe481fa21c3/node_modules/@hono/standard-validator/dist/index.js
2797
+ // ../../node_modules/.bun/@hono+standard-validator@0.2.2+ba5f02f09be4faa7/node_modules/@hono/standard-validator/dist/index.js
2769
2798
  var RESTRICTED_DATA_FIELDS = { header: ["cookie"] };
2770
2799
  function sanitizeIssues(issues, vendor, target) {
2771
2800
  if (!(target in RESTRICTED_DATA_FIELDS))
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.20",
3
+ "version": "0.1.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -29,13 +29,13 @@
29
29
  "build-types": "tsc --project tsconfig.build.json"
30
30
  },
31
31
  "dependencies": {
32
- "@nice-code/error": "0.0.20",
32
+ "@nice-code/error": "0.1.0",
33
33
  "@standard-schema/spec": "^1.1.0",
34
34
  "@hono/standard-validator": "^0.2.2",
35
35
  "http-status-codes": "^2.3.0"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "valibot": "^1.3.1",
39
- "hono": "^4.12.14"
39
+ "hono": "^4.12.15"
40
40
  }
41
41
  }