@vertz/errors 0.2.15 → 0.2.16

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.
Files changed (3) hide show
  1. package/dist/index.d.ts +478 -478
  2. package/dist/index.js +259 -259
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -303,207 +303,125 @@ function createValidationError2(message, issues) {
303
303
  function isValidationError2(error) {
304
304
  return error.code === "VALIDATION_FAILED";
305
305
  }
306
- // src/infra/index.ts
307
- class InfraError extends Error {
308
- constructor(message) {
306
+ // src/entity.ts
307
+ class EntityError extends Error {
308
+ code;
309
+ constructor(code, message) {
309
310
  super(message);
311
+ this.code = code;
310
312
  this.name = this.constructor.name;
311
313
  }
312
314
  }
313
315
 
314
- class ConnectionError extends InfraError {
315
- constructor(message = "Database connection failed") {
316
- super(message);
316
+ class BadRequestError extends EntityError {
317
+ code = "BadRequest";
318
+ constructor(message = "Bad Request") {
319
+ super("BadRequest", message);
320
+ this.name = "BadRequestError";
317
321
  }
318
322
  }
319
-
320
- class PoolExhaustedError extends InfraError {
321
- constructor(message = "Database pool exhausted") {
322
- super(message);
323
- }
323
+ function isBadRequestError(error) {
324
+ return error instanceof BadRequestError;
324
325
  }
325
326
 
326
- class QueryError extends InfraError {
327
- constructor(message = "Query execution failed") {
328
- super(message);
327
+ class EntityUnauthorizedError extends EntityError {
328
+ code = "Unauthorized";
329
+ constructor(message = "Unauthorized") {
330
+ super("Unauthorized", message);
331
+ this.name = "UnauthorizedError";
329
332
  }
330
333
  }
331
-
332
- class TimeoutError extends InfraError {
333
- constructor(message = "Operation timed out") {
334
- super(message);
335
- }
334
+ function isEntityUnauthorizedError(error) {
335
+ return error instanceof EntityUnauthorizedError;
336
336
  }
337
337
 
338
- class NetworkError extends InfraError {
339
- constructor(message = "Network request failed") {
340
- super(message);
338
+ class EntityForbiddenError extends EntityError {
339
+ code = "Forbidden";
340
+ constructor(message = "Forbidden") {
341
+ super("Forbidden", message);
342
+ this.name = "ForbiddenError";
341
343
  }
342
344
  }
343
-
344
- class SerializationError extends InfraError {
345
- constructor(message = "Failed to decode response") {
346
- super(message);
347
- }
345
+ function isEntityForbiddenError(error) {
346
+ return error instanceof EntityForbiddenError;
348
347
  }
349
- // src/mapping/db-to-http.ts
350
- function dbErrorToHttpStatus(error) {
351
- const code = error.code;
352
- switch (code) {
353
- case "NotFound":
354
- return 404;
355
- case "UNIQUE_VIOLATION":
356
- return 409;
357
- case "FK_VIOLATION":
358
- return 422;
359
- case "NOT_NULL_VIOLATION":
360
- return 422;
361
- case "CHECK_VIOLATION":
362
- return 422;
363
- default:
364
- return 500;
348
+
349
+ class EntityNotFoundError extends EntityError {
350
+ code = "NotFound";
351
+ resource;
352
+ resourceId;
353
+ constructor(message = "Not Found", resource, resourceId) {
354
+ super("NotFound", message);
355
+ this.name = "NotFoundError";
356
+ this.resource = resource;
357
+ this.resourceId = resourceId;
365
358
  }
366
359
  }
367
- function notFoundErrorToHttpStatus(_error) {
368
- return 404;
369
- }
370
- function uniqueViolationToHttpStatus(_error) {
371
- return 409;
372
- }
373
- function fkViolationToHttpStatus(_error) {
374
- return 422;
375
- }
376
- function notNullViolationToHttpStatus(_error) {
377
- return 422;
378
- }
379
- function checkViolationToHttpStatus(_error) {
380
- return 422;
381
- }
382
- // src/mapping/http-to-client.ts
383
- function parseUnknownError(status, body) {
384
- const message = typeof body === "object" && body !== null && "message" in body ? String(body.message) : "Request failed";
385
- return {
386
- code: "UNKNOWN",
387
- message,
388
- status
389
- };
360
+ function isEntityNotFoundError(error) {
361
+ return error instanceof EntityNotFoundError;
390
362
  }
391
- function httpToClientError(status, body) {
392
- if (body === null || body === undefined || body === "") {
393
- return parseUnknownError(status, body);
394
- }
395
- if (typeof body !== "object") {
396
- return parseUnknownError(status, body);
397
- }
398
- const bodyObj = body;
399
- const message = typeof bodyObj.message === "string" ? bodyObj.message : "Request failed";
400
- switch (status) {
401
- case 400:
402
- if (bodyObj.code === "VALIDATION_FAILED" || bodyObj.issues) {
403
- const error = {
404
- code: "ValidationError",
405
- message,
406
- issues: Array.isArray(bodyObj.issues) ? bodyObj.issues : undefined
407
- };
408
- return error;
409
- }
410
- return parseUnknownError(status, body);
411
- case 401:
412
- return {
413
- code: "Unauthorized",
414
- message
415
- };
416
- case 403:
417
- return {
418
- code: "Forbidden",
419
- message
420
- };
421
- case 404:
422
- return {
423
- code: "NotFound",
424
- message,
425
- resource: typeof bodyObj.resource === "string" ? bodyObj.resource : undefined
426
- };
427
- case 409:
428
- return {
429
- code: "Conflict",
430
- message,
431
- field: typeof bodyObj.field === "string" ? bodyObj.field : undefined
432
- };
433
- case 422:
434
- if (bodyObj.code === "VALIDATION_FAILED" || bodyObj.issues) {
435
- const error = {
436
- code: "ValidationError",
437
- message,
438
- issues: Array.isArray(bodyObj.issues) ? bodyObj.issues : undefined
439
- };
440
- return error;
441
- }
442
- case 429:
443
- return {
444
- code: "RATE_LIMITED",
445
- message,
446
- retryAfter: typeof bodyObj.retryAfter === "number" ? bodyObj.retryAfter : undefined
447
- };
448
- case 500:
449
- case 502:
450
- case 503:
451
- case 504:
452
- return parseUnknownError(status, body);
453
- default:
454
- return parseUnknownError(status, body);
363
+
364
+ class MethodNotAllowedError extends EntityError {
365
+ code = "MethodNotAllowed";
366
+ allowedMethods;
367
+ constructor(allowedMethods, message = "Method Not Allowed") {
368
+ super("MethodNotAllowed", message);
369
+ this.name = "MethodNotAllowedError";
370
+ this.allowedMethods = allowedMethods;
455
371
  }
456
372
  }
457
- function isUnknownError(error) {
458
- return error.code === "UNKNOWN";
373
+ function isMethodNotAllowedError(error) {
374
+ return error instanceof MethodNotAllowedError;
459
375
  }
460
- // src/result.ts
461
- var ok = (data) => ({ ok: true, data });
462
- var err = (error) => ({ ok: false, error });
463
- function unwrap(result) {
464
- if (result.ok) {
465
- return result.data;
376
+
377
+ class EntityConflictError extends EntityError {
378
+ code = "Conflict";
379
+ field;
380
+ constructor(message = "Conflict", field) {
381
+ super("Conflict", message);
382
+ this.name = "ConflictError";
383
+ this.field = field;
466
384
  }
467
- throw result.error;
468
385
  }
469
- function unwrapOr(result, defaultValue) {
470
- if (result.ok) {
471
- return result.data;
472
- }
473
- return defaultValue;
386
+ function isEntityConflictError(error) {
387
+ return error instanceof EntityConflictError;
474
388
  }
475
- function map(result, fn) {
476
- if (result.ok) {
477
- return { ok: true, data: fn(result.data) };
389
+
390
+ class EntityValidationError extends EntityError {
391
+ code = "ValidationError";
392
+ errors;
393
+ constructor(errors) {
394
+ super("ValidationError", "Validation failed");
395
+ this.name = "EntityValidationError";
396
+ this.errors = errors;
478
397
  }
479
- return result;
480
398
  }
481
- function flatMap(result, fn) {
482
- if (result.ok) {
483
- return fn(result.data);
399
+ function isEntityValidationError(error) {
400
+ return error instanceof EntityValidationError;
401
+ }
402
+
403
+ class InternalError extends EntityError {
404
+ code = "InternalError";
405
+ constructor(message = "Internal Server Error") {
406
+ super("InternalError", message);
407
+ this.name = "InternalError";
484
408
  }
485
- return result;
486
409
  }
487
- function match(result, handlers) {
488
- return result.ok ? handlers.ok(result.data) : handlers.err(result.error);
410
+ function isInternalError(error) {
411
+ return error instanceof InternalError;
489
412
  }
490
- function matchErr(result, handlers) {
491
- if (result.ok) {
492
- return handlers.ok(result.data);
493
- }
494
- const errorCode = result.error.code;
495
- const handlersRecord = handlers;
496
- const handler = handlersRecord[errorCode];
497
- if (!handler) {
498
- throw new Error(`Unhandled error code: ${errorCode}`);
413
+
414
+ class ServiceUnavailableError extends EntityError {
415
+ code = "ServiceUnavailable";
416
+ retryAfter;
417
+ constructor(message = "Service Unavailable", retryAfter) {
418
+ super("ServiceUnavailable", message);
419
+ this.name = "ServiceUnavailableError";
420
+ this.retryAfter = retryAfter;
499
421
  }
500
- return handler(result.error);
501
- }
502
- function isOk(result) {
503
- return result.ok === true;
504
422
  }
505
- function isErr(result) {
506
- return result.ok === false;
423
+ function isServiceUnavailableError(error) {
424
+ return error instanceof ServiceUnavailableError;
507
425
  }
508
426
  // src/fetch.ts
509
427
  class FetchError extends Error {
@@ -715,125 +633,159 @@ function createHttpError(status, message, serverCode) {
715
633
  return new HttpError(status, message, serverCode);
716
634
  }
717
635
  }
718
- // src/entity.ts
719
- class EntityError extends Error {
720
- code;
721
- constructor(code, message) {
636
+ // src/infra/index.ts
637
+ class InfraError extends Error {
638
+ constructor(message) {
722
639
  super(message);
723
- this.code = code;
724
640
  this.name = this.constructor.name;
725
641
  }
726
642
  }
727
643
 
728
- class BadRequestError extends EntityError {
729
- code = "BadRequest";
730
- constructor(message = "Bad Request") {
731
- super("BadRequest", message);
732
- this.name = "BadRequestError";
644
+ class ConnectionError extends InfraError {
645
+ constructor(message = "Database connection failed") {
646
+ super(message);
733
647
  }
734
648
  }
735
- function isBadRequestError(error) {
736
- return error instanceof BadRequestError;
737
- }
738
649
 
739
- class EntityUnauthorizedError extends EntityError {
740
- code = "Unauthorized";
741
- constructor(message = "Unauthorized") {
742
- super("Unauthorized", message);
743
- this.name = "UnauthorizedError";
650
+ class PoolExhaustedError extends InfraError {
651
+ constructor(message = "Database pool exhausted") {
652
+ super(message);
744
653
  }
745
654
  }
746
- function isEntityUnauthorizedError(error) {
747
- return error instanceof EntityUnauthorizedError;
748
- }
749
655
 
750
- class EntityForbiddenError extends EntityError {
751
- code = "Forbidden";
752
- constructor(message = "Forbidden") {
753
- super("Forbidden", message);
754
- this.name = "ForbiddenError";
656
+ class QueryError extends InfraError {
657
+ constructor(message = "Query execution failed") {
658
+ super(message);
755
659
  }
756
660
  }
757
- function isEntityForbiddenError(error) {
758
- return error instanceof EntityForbiddenError;
759
- }
760
661
 
761
- class EntityNotFoundError extends EntityError {
762
- code = "NotFound";
763
- resource;
764
- resourceId;
765
- constructor(message = "Not Found", resource, resourceId) {
766
- super("NotFound", message);
767
- this.name = "NotFoundError";
768
- this.resource = resource;
769
- this.resourceId = resourceId;
662
+ class TimeoutError extends InfraError {
663
+ constructor(message = "Operation timed out") {
664
+ super(message);
770
665
  }
771
666
  }
772
- function isEntityNotFoundError(error) {
773
- return error instanceof EntityNotFoundError;
774
- }
775
667
 
776
- class MethodNotAllowedError extends EntityError {
777
- code = "MethodNotAllowed";
778
- allowedMethods;
779
- constructor(allowedMethods, message = "Method Not Allowed") {
780
- super("MethodNotAllowed", message);
781
- this.name = "MethodNotAllowedError";
782
- this.allowedMethods = allowedMethods;
668
+ class NetworkError extends InfraError {
669
+ constructor(message = "Network request failed") {
670
+ super(message);
783
671
  }
784
672
  }
785
- function isMethodNotAllowedError(error) {
786
- return error instanceof MethodNotAllowedError;
787
- }
788
673
 
789
- class EntityConflictError extends EntityError {
790
- code = "Conflict";
791
- field;
792
- constructor(message = "Conflict", field) {
793
- super("Conflict", message);
794
- this.name = "ConflictError";
795
- this.field = field;
674
+ class SerializationError extends InfraError {
675
+ constructor(message = "Failed to decode response") {
676
+ super(message);
796
677
  }
797
678
  }
798
- function isEntityConflictError(error) {
799
- return error instanceof EntityConflictError;
800
- }
801
-
802
- class EntityValidationError extends EntityError {
803
- code = "ValidationError";
804
- errors;
805
- constructor(errors) {
806
- super("ValidationError", "Validation failed");
807
- this.name = "EntityValidationError";
808
- this.errors = errors;
679
+ // src/mapping/db-to-http.ts
680
+ function dbErrorToHttpStatus(error) {
681
+ const code = error.code;
682
+ switch (code) {
683
+ case "NotFound":
684
+ return 404;
685
+ case "UNIQUE_VIOLATION":
686
+ return 409;
687
+ case "FK_VIOLATION":
688
+ return 422;
689
+ case "NOT_NULL_VIOLATION":
690
+ return 422;
691
+ case "CHECK_VIOLATION":
692
+ return 422;
693
+ default:
694
+ return 500;
809
695
  }
810
696
  }
811
- function isEntityValidationError(error) {
812
- return error instanceof EntityValidationError;
697
+ function notFoundErrorToHttpStatus(_error) {
698
+ return 404;
813
699
  }
814
-
815
- class InternalError extends EntityError {
816
- code = "InternalError";
817
- constructor(message = "Internal Server Error") {
818
- super("InternalError", message);
819
- this.name = "InternalError";
820
- }
700
+ function uniqueViolationToHttpStatus(_error) {
701
+ return 409;
821
702
  }
822
- function isInternalError(error) {
823
- return error instanceof InternalError;
703
+ function fkViolationToHttpStatus(_error) {
704
+ return 422;
824
705
  }
825
-
826
- class ServiceUnavailableError extends EntityError {
827
- code = "ServiceUnavailable";
828
- retryAfter;
829
- constructor(message = "Service Unavailable", retryAfter) {
830
- super("ServiceUnavailable", message);
831
- this.name = "ServiceUnavailableError";
832
- this.retryAfter = retryAfter;
706
+ function notNullViolationToHttpStatus(_error) {
707
+ return 422;
708
+ }
709
+ function checkViolationToHttpStatus(_error) {
710
+ return 422;
711
+ }
712
+ // src/mapping/http-to-client.ts
713
+ function parseUnknownError(status, body) {
714
+ const message = typeof body === "object" && body !== null && "message" in body ? String(body.message) : "Request failed";
715
+ return {
716
+ code: "UNKNOWN",
717
+ message,
718
+ status
719
+ };
720
+ }
721
+ function httpToClientError(status, body) {
722
+ if (body === null || body === undefined || body === "") {
723
+ return parseUnknownError(status, body);
724
+ }
725
+ if (typeof body !== "object") {
726
+ return parseUnknownError(status, body);
727
+ }
728
+ const bodyObj = body;
729
+ const message = typeof bodyObj.message === "string" ? bodyObj.message : "Request failed";
730
+ switch (status) {
731
+ case 400:
732
+ if (bodyObj.code === "VALIDATION_FAILED" || bodyObj.issues) {
733
+ const error = {
734
+ code: "ValidationError",
735
+ message,
736
+ issues: Array.isArray(bodyObj.issues) ? bodyObj.issues : undefined
737
+ };
738
+ return error;
739
+ }
740
+ return parseUnknownError(status, body);
741
+ case 401:
742
+ return {
743
+ code: "Unauthorized",
744
+ message
745
+ };
746
+ case 403:
747
+ return {
748
+ code: "Forbidden",
749
+ message
750
+ };
751
+ case 404:
752
+ return {
753
+ code: "NotFound",
754
+ message,
755
+ resource: typeof bodyObj.resource === "string" ? bodyObj.resource : undefined
756
+ };
757
+ case 409:
758
+ return {
759
+ code: "Conflict",
760
+ message,
761
+ field: typeof bodyObj.field === "string" ? bodyObj.field : undefined
762
+ };
763
+ case 422:
764
+ if (bodyObj.code === "VALIDATION_FAILED" || bodyObj.issues) {
765
+ const error = {
766
+ code: "ValidationError",
767
+ message,
768
+ issues: Array.isArray(bodyObj.issues) ? bodyObj.issues : undefined
769
+ };
770
+ return error;
771
+ }
772
+ case 429:
773
+ return {
774
+ code: "RATE_LIMITED",
775
+ message,
776
+ retryAfter: typeof bodyObj.retryAfter === "number" ? bodyObj.retryAfter : undefined
777
+ };
778
+ case 500:
779
+ case 502:
780
+ case 503:
781
+ case 504:
782
+ return parseUnknownError(status, body);
783
+ default:
784
+ return parseUnknownError(status, body);
833
785
  }
834
786
  }
835
- function isServiceUnavailableError(error) {
836
- return error instanceof ServiceUnavailableError;
787
+ function isUnknownError(error) {
788
+ return error.code === "UNKNOWN";
837
789
  }
838
790
  // src/match-error.ts
839
791
  function matchError(error, handlers) {
@@ -879,6 +831,54 @@ function matchError(error, handlers) {
879
831
  };
880
832
  return checkExhaustive(code);
881
833
  }
834
+ // src/result.ts
835
+ var ok = (data) => ({ ok: true, data });
836
+ var err = (error) => ({ ok: false, error });
837
+ function unwrap(result) {
838
+ if (result.ok) {
839
+ return result.data;
840
+ }
841
+ throw result.error;
842
+ }
843
+ function unwrapOr(result, defaultValue) {
844
+ if (result.ok) {
845
+ return result.data;
846
+ }
847
+ return defaultValue;
848
+ }
849
+ function map(result, fn) {
850
+ if (result.ok) {
851
+ return { ok: true, data: fn(result.data) };
852
+ }
853
+ return result;
854
+ }
855
+ function flatMap(result, fn) {
856
+ if (result.ok) {
857
+ return fn(result.data);
858
+ }
859
+ return result;
860
+ }
861
+ function match(result, handlers) {
862
+ return result.ok ? handlers.ok(result.data) : handlers.err(result.error);
863
+ }
864
+ function matchErr(result, handlers) {
865
+ if (result.ok) {
866
+ return handlers.ok(result.data);
867
+ }
868
+ const errorCode = result.error.code;
869
+ const handlersRecord = handlers;
870
+ const handler = handlersRecord[errorCode];
871
+ if (!handler) {
872
+ throw new Error(`Unhandled error code: ${errorCode}`);
873
+ }
874
+ return handler(result.error);
875
+ }
876
+ function isOk(result) {
877
+ return result.ok === true;
878
+ }
879
+ function isErr(result) {
880
+ return result.ok === false;
881
+ }
882
882
  export {
883
883
  unwrapOr,
884
884
  unwrap,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vertz/errors",
3
- "version": "0.2.15",
3
+ "version": "0.2.16",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Unified error taxonomy for Vertz - Result types, domain errors, and mapping utilities",