No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

errors.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.assert = exports.NotImplementedError = exports.InternalError = exports.UnknownError = exports.NotFoundError = exports.PermissionDeniedError = exports.UnauthenticatedError = exports.InvalidArgumentError = exports.BadRequestError = exports.ApiError = void 0;
  4. const errors_1 = require("exegesis/lib/errors");
  5. class ApiError extends errors_1.ExtendableError {
  6. constructor(code, status, message, reasonOrErrors) {
  7. super(message);
  8. this.code = code;
  9. this.status = status;
  10. this.code = code;
  11. this.status = status;
  12. if (typeof reasonOrErrors === "string") {
  13. this.errors = [{ message, reason: reasonOrErrors }];
  14. }
  15. else {
  16. this.errors = reasonOrErrors;
  17. }
  18. }
  19. toJSON() {
  20. return { code: this.code, message: this.message, errors: this.errors, status: this.status };
  21. }
  22. }
  23. exports.ApiError = ApiError;
  24. class BadRequestError extends ApiError {
  25. constructor(message, reasonOrErrors = [{ message, reason: "invalid", domain: "global" }]) {
  26. super(400, undefined, message, reasonOrErrors);
  27. }
  28. }
  29. exports.BadRequestError = BadRequestError;
  30. class InvalidArgumentError extends ApiError {
  31. constructor(message, reasonOrErrors = [{ message, reason: "invalid", domain: "global" }]) {
  32. super(400, "INVALID_ARGUMENT", message, reasonOrErrors);
  33. }
  34. }
  35. exports.InvalidArgumentError = InvalidArgumentError;
  36. class UnauthenticatedError extends ApiError {
  37. constructor(message, reasonOrErrors) {
  38. super(401, "UNAUTHENTICATED", message, reasonOrErrors);
  39. }
  40. }
  41. exports.UnauthenticatedError = UnauthenticatedError;
  42. class PermissionDeniedError extends ApiError {
  43. constructor(message, reasonOrErrors = [{ message, reason: "forbidden", domain: "global" }]) {
  44. super(403, "PERMISSION_DENIED", message, reasonOrErrors);
  45. }
  46. }
  47. exports.PermissionDeniedError = PermissionDeniedError;
  48. class NotFoundError extends ApiError {
  49. constructor(message = "Not Found", reasonOrErrors = "notFound") {
  50. super(404, "NOT_FOUND", message, reasonOrErrors);
  51. }
  52. }
  53. exports.NotFoundError = NotFoundError;
  54. class UnknownError extends ApiError {
  55. constructor(message, reason) {
  56. super(500, "UNKNOWN", message, reason);
  57. }
  58. }
  59. exports.UnknownError = UnknownError;
  60. class InternalError extends ApiError {
  61. constructor(message, reason) {
  62. super(500, "INTERNAL", message, reason);
  63. }
  64. }
  65. exports.InternalError = InternalError;
  66. class NotImplementedError extends ApiError {
  67. constructor(message, reason = "unimplemented") {
  68. super(501, "NOT_IMPLEMENTED", message, reason);
  69. }
  70. }
  71. exports.NotImplementedError = NotImplementedError;
  72. function assert(assertion, error) {
  73. if (!assertion) {
  74. throw new BadRequestError(error);
  75. }
  76. }
  77. exports.assert = assert;