@lowerdeck/error 1.0.9 → 1.0.10
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/defaultErrors.d.ts.map +1 -1
- package/dist/error.d.ts +6 -5
- package/dist/error.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.module.js +1 -1
- package/dist/index.module.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"defaultErrors.d.ts","sourceRoot":"","sources":["../src/defaultErrors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEjD,eAAO,IAAI,eAAe,GACxB,GAAG;IAAE,MAAM,EAAE,eAAe,EAAE,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAAC,SAAS,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC,uDAQxF,CAAC;AAEL,eAAO,IAAI,mBAAmB,6DAI5B,CAAC;AAEH,eAAO,IAAI,eAAe,mDAIxB,CAAC;AAEH,wBAAgB,aAAa,
|
|
1
|
+
{"version":3,"file":"defaultErrors.d.ts","sourceRoot":"","sources":["../src/defaultErrors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEjD,eAAO,IAAI,eAAe,GACxB,GAAG;IAAE,MAAM,EAAE,eAAe,EAAE,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAAC,SAAS,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC,uDAQxF,CAAC;AAEL,eAAO,IAAI,mBAAmB,6DAI5B,CAAC;AAEH,eAAO,IAAI,eAAe,mDAIxB,CAAC;AAEH,wBAAgB,aAAa,CAC3B,EAAE,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,EAAE,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAAC,SAAS,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,GACzE,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAClC,wBAAgB,aAAa,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAiB9F,eAAO,IAAI,iBAAiB,oDAK1B,CAAC;AAEH,eAAO,IAAI,cAAc,iDAIvB,CAAC;AAEH,eAAO,IAAI,cAAc,uDAIvB,CAAC;AAEH,eAAO,IAAI,aAAa,gDAItB,CAAC;AAEH,eAAO,IAAI,SAAS,4CAIlB,CAAC;AAEH,eAAO,IAAI,oBAAoB,wDAI7B,CAAC;AAEH,eAAO,IAAI,uBAAuB,2DAIhC,CAAC;AAEH,eAAO,IAAI,kBAAkB,sDAI3B,CAAC;AAEH,eAAO,IAAI,mBAAmB,uDAI5B,CAAC;AAEH,eAAO,IAAI,oBAAoB,yDAK7B,CAAC;AAEH,eAAO,IAAI,YAAY,+CAIrB,CAAC;AAEH,eAAO,IAAI,qBAAqB,0DAI9B,CAAC"}
|
package/dist/error.d.ts
CHANGED
|
@@ -7,25 +7,26 @@ export interface ErrorData<Code extends string, Status extends number> {
|
|
|
7
7
|
reason?: string;
|
|
8
8
|
[key: string]: any;
|
|
9
9
|
}
|
|
10
|
+
export type ErrorRecordExtension<Code extends string, Status extends number> = <NewCode extends string = Code>(extension?: Partial<ErrorData<NewCode, Status>>) => ErrorRecord<NewCode, Status>;
|
|
10
11
|
export type ErrorRecord<Code extends string, Status extends number> = {
|
|
11
|
-
|
|
12
|
+
object: 'error';
|
|
12
13
|
data: ErrorData<Code, Status>;
|
|
13
14
|
toResponse: () => ErrorData<Code, Status> & {
|
|
14
|
-
|
|
15
|
+
object: 'error';
|
|
15
16
|
ok: false;
|
|
16
17
|
};
|
|
17
|
-
} &
|
|
18
|
+
} & ErrorRecordExtension<Code, Status>;
|
|
18
19
|
export declare let createError: <Code extends string, Status extends number>(data: ErrorData<Code, Status>) => ErrorRecord<Code, Status>;
|
|
19
20
|
export declare class ServiceError<InnerError extends ErrorRecord<any, any>> extends Error {
|
|
20
21
|
private readonly error;
|
|
21
|
-
|
|
22
|
+
object: "ServiceError";
|
|
22
23
|
private _parent;
|
|
23
24
|
get parent(): Error | null;
|
|
24
25
|
constructor(error: InnerError);
|
|
25
26
|
setParent(parent: Error): this;
|
|
26
27
|
get data(): ErrorData<any, any>;
|
|
27
28
|
toResponse(): ErrorData<any, any> & {
|
|
28
|
-
|
|
29
|
+
object: "error";
|
|
29
30
|
ok: false;
|
|
30
31
|
};
|
|
31
32
|
static fromResponse(raw: ErrorData<any, any>): ServiceError<ErrorRecord<any, any>>;
|
package/dist/error.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,SAAS,CAAC,IAAI,SAAS,MAAM,EAAE,MAAM,SAAS,MAAM;IACnE,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,IAAI,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,SAAS,CAAC,IAAI,SAAS,MAAM,EAAE,MAAM,SAAS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IAErC,IAAW,MAAM,iBAEhB;gBAE4B,KAAK,EAAE,UAAU;IAI9C,SAAS,CAAC,MAAM,EAAE,KAAK;IAKvB,IAAI,IAAI,wBAEP;IAED,UAAU;gBA5C4C,OAAO;YAAM,KAAK;;IAgDxE,MAAM,CAAC,YAAY,CAAC,GAAG,EAAE,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;CAO7C;AAED,eAAO,IAAI,cAAc,GAAI,GAAG,GAAG,KAAG,CAAC,IAAI,YAAY,CAAC,GAAG,CAE1D,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("@lowerdeck/case");function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var o in r)({}).hasOwnProperty.call(r,o)&&(e[o]=r[o])}return e},t.apply(null,arguments)}function r(e){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},r(e)}function o(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(o=function(){return!!e})()}function n(e,t){return n=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},n(e,t)}function s(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}function i(e){var t="function"==typeof Map?new Map:void 0;return i=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,s)}function s(){return function(e,t,r){if(o())return Reflect.construct.apply(null,arguments);var s=[null];s.push.apply(s,t);var i=new(e.bind.apply(e,s));return r&&n(i,r.prototype),i}(e,arguments,r(this).constructor)}return s.prototype=Object.create(e.prototype,{constructor:{value:s,enumerable:!1,writable:!0,configurable:!0}}),n(s,e)},i(e)}var a=function(e){return Object.assign(function(r){return void 0===r&&(r={}),a(t({},e,r))},{
|
|
1
|
+
var e=require("@lowerdeck/case");function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var o in r)({}).hasOwnProperty.call(r,o)&&(e[o]=r[o])}return e},t.apply(null,arguments)}function r(e){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},r(e)}function o(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(o=function(){return!!e})()}function n(e,t){return n=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},n(e,t)}function s(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}function i(e){var t="function"==typeof Map?new Map:void 0;return i=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,s)}function s(){return function(e,t,r){if(o())return Reflect.construct.apply(null,arguments);var s=[null];s.push.apply(s,t);var i=new(e.bind.apply(e,s));return r&&n(i,r.prototype),i}(e,arguments,r(this).constructor)}return s.prototype=Object.create(e.prototype,{constructor:{value:s,enumerable:!1,writable:!0,configurable:!0}}),n(s,e)},i(e)}var a=function(e){return Object.assign(function(r){return void 0===r&&(r={}),a(t({},e,r))},{object:"error",data:e,toResponse:function(){return t({object:"error",ok:!1},e)}})},u=/*#__PURE__*/function(e){function t(t){var r;return(r=e.call(this,t.data.message)||this).error=void 0,r.object="ServiceError",r._parent=null,r.error=t,r}var r,o;o=e,(r=t).prototype=Object.create(o.prototype),r.prototype.constructor=r,n(r,o);var i,u,c=t.prototype;return c.setParent=function(e){return this._parent=e,this},c.toResponse=function(){return this.error.toResponse()},t.fromResponse=function(e){var r=e;return delete r.ok,delete r.object,new t(a(r))},i=t,(u=[{key:"parent",get:function(){return this._parent}},{key:"data",get:function(){return this.error.data}}])&&function(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,s(o.key),o)}}(i.prototype,u),Object.defineProperty(i,"prototype",{writable:!1}),i}(/*#__PURE__*/i(Error)),c=a({status:500,code:"internal_server_error",message:"An internal server error occurred."}),d=a({status:400,code:"bad_request",message:"The request is invalid."}),p=a({status:401,code:"unauthorized",message:"You are not authorized to access this resource.",hint:"Make sure you are logged in and have the correct permissions to access this resource."}),l=a({status:403,code:"forbidden",message:"You do not have permission to access this resource."}),f=a({status:400,code:"invalid_version",message:"The endpoint does not support the requested version."}),h=a({status:409,code:"conflict",message:"A similar resource already exists."}),v=a({status:410,code:"gone",message:"The requested resource is no longer available."}),y=a({status:402,code:"payment_required",message:"Payment is required to access this resource."}),m=a({status:412,code:"precondition_failed",message:"The precondition of the request failed."}),b=a({status:406,code:"not_acceptable",message:"The requested resource is not acceptable."}),g=a({status:501,code:"not_implemented",message:"The requested resource is not implemented."}),_=a({status:429,code:"too_many_requests",message:"You have made too many requests in a short period of time.",hint:"Please wait a moment and try again."}),E=a({status:504,code:"timeout",message:"The request timed out."}),x=a({status:405,code:"method_not_allowed",message:"The requested method is not allowed for this resource."});exports.ServiceError=u,exports.badRequestError=d,exports.conflictError=h,exports.createError=a,exports.forbiddenError=l,exports.goneError=v,exports.internalServerError=c,exports.invalidVersion=f,exports.isServiceError=function(e){return"ServiceError"===(null==e?void 0:e.object)||"ErrorRecord"===(null==e?void 0:e.object)},exports.methodNotAllowedError=x,exports.notAcceptableError=b,exports.notFoundError=function(r,o){var n="string"==typeof r?r:r.entity,s="string"==typeof r?o:r.id;return a(t({status:404,code:"not_found",message:"The requested "+e.Cases.toKebabCase(n)+" could not be found.",hint:"Make sure the resource you are trying to access exists, that you have access to it and that it has not been deleted.",entity:n,id:null!=s?s:void 0},"string"==typeof r?{}:r))},exports.notImplementedError=g,exports.paymentRequiredError=y,exports.preconditionFailedError=m,exports.timeoutError=E,exports.tooManyRequestsError=_,exports.unauthorizedError=p,exports.validationError=function(e){return a(t({status:400,code:"invalid_data",message:"The provided data is invalid.",hint:"Make sure the data you are sending follows the specification outlined in the documentation."},e))};
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/error.ts","../src/defaultErrors.ts"],"sourcesContent":["export interface ErrorData<Code extends string, Status extends number> {\n status: Status;\n code: Code;\n message: string;\n hint?: string;\n description?: string;\n reason?: string;\n [key: string]: any;\n}\n\nexport type ErrorRecord<Code extends string, Status extends number> = {\n
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/error.ts","../src/defaultErrors.ts"],"sourcesContent":["export interface ErrorData<Code extends string, Status extends number> {\n status: Status;\n code: Code;\n message: string;\n hint?: string;\n description?: string;\n reason?: string;\n [key: string]: any;\n}\n\nexport type ErrorRecordExtension<Code extends string, Status extends number> = <\n NewCode extends string = Code\n>(\n extension?: Partial<ErrorData<NewCode, Status>>\n) => ErrorRecord<NewCode, Status>;\n\nexport type ErrorRecord<Code extends string, Status extends number> = {\n object: 'error';\n data: ErrorData<Code, Status>;\n toResponse: () => ErrorData<Code, Status> & { object: 'error'; ok: false };\n} & ErrorRecordExtension<Code, Status>;\n\nexport let createError = <Code extends string, Status extends number>(\n data: ErrorData<Code, Status>\n): ErrorRecord<Code, Status> => {\n return Object.assign(\n <NewCode extends string = Code>(\n extension: Partial<ErrorData<NewCode, Status>> = {}\n ): ErrorRecord<NewCode, Status> =>\n createError({\n ...data,\n ...extension\n }) as any,\n {\n object: 'error' as const,\n data,\n toResponse: () => ({ object: 'error' as const, ok: false as const, ...data })\n }\n ) as any;\n};\n\nexport class ServiceError<InnerError extends ErrorRecord<any, any>> extends Error {\n object = 'ServiceError' as const;\n\n private _parent: Error | null = null;\n\n public get parent() {\n return this._parent;\n }\n\n constructor(private readonly error: InnerError) {\n super(error.data.message);\n }\n\n setParent(parent: Error) {\n this._parent = parent;\n return this;\n }\n\n get data() {\n return this.error.data;\n }\n\n toResponse() {\n return this.error.toResponse();\n }\n\n static fromResponse(raw: ErrorData<any, any>) {\n let data = raw;\n delete data.ok;\n delete data.object;\n\n return new ServiceError(createError(data));\n }\n}\n\nexport let isServiceError = (e: any): e is ServiceError<any> => {\n return e?.object === 'ServiceError' || e?.object === 'ErrorRecord';\n};\n","import { Cases } from '@lowerdeck/case';\nimport { ValidationError } from '@lowerdeck/validation';\nimport { ErrorData, createError } from './error';\n\nexport let validationError = (\n d: { errors: ValidationError[]; entity: string } & Partial<ErrorData<'invalid_data', 400>>\n) =>\n createError({\n status: 400,\n code: 'invalid_data',\n message: 'The provided data is invalid.',\n hint: 'Make sure the data you are sending follows the specification outlined in the documentation.',\n ...d\n });\n\nexport let internalServerError = createError({\n status: 500,\n code: 'internal_server_error',\n message: 'An internal server error occurred.'\n});\n\nexport let badRequestError = createError({\n status: 400,\n code: 'bad_request',\n message: 'The request is invalid.'\n});\n\nexport function notFoundError(\n p1: { entity: string; id?: string } & Partial<ErrorData<'not_found', 404>>\n): ReturnType<typeof createError>;\nexport function notFoundError(p1: string, p2?: string | null): ReturnType<typeof createError>;\nexport function notFoundError(p1: any, p2?: any): ReturnType<typeof createError> {\n let entity = typeof p1 == 'string' ? p1 : p1.entity;\n let id = typeof p1 == 'string' ? p2 : p1.id;\n\n return createError({\n status: 404,\n code: 'not_found',\n message: `The requested ${Cases.toKebabCase(entity)} could not be found.`,\n hint: 'Make sure the resource you are trying to access exists, that you have access to it and that it has not been deleted.',\n entity,\n id: id ?? undefined,\n\n ...(typeof p1 == 'string' ? {} : p1)\n });\n}\n\nexport let unauthorizedError = createError({\n status: 401,\n code: 'unauthorized',\n message: 'You are not authorized to access this resource.',\n hint: 'Make sure you are logged in and have the correct permissions to access this resource.'\n});\n\nexport let forbiddenError = createError({\n status: 403,\n code: 'forbidden',\n message: 'You do not have permission to access this resource.'\n});\n\nexport let invalidVersion = createError({\n status: 400,\n code: 'invalid_version',\n message: 'The endpoint does not support the requested version.'\n});\n\nexport let conflictError = createError({\n status: 409,\n code: 'conflict',\n message: 'A similar resource already exists.'\n});\n\nexport let goneError = createError({\n status: 410,\n code: 'gone',\n message: 'The requested resource is no longer available.'\n});\n\nexport let paymentRequiredError = createError({\n status: 402,\n code: 'payment_required',\n message: 'Payment is required to access this resource.'\n});\n\nexport let preconditionFailedError = createError({\n status: 412,\n code: 'precondition_failed',\n message: 'The precondition of the request failed.'\n});\n\nexport let notAcceptableError = createError({\n status: 406,\n code: 'not_acceptable',\n message: 'The requested resource is not acceptable.'\n});\n\nexport let notImplementedError = createError({\n status: 501,\n code: 'not_implemented',\n message: 'The requested resource is not implemented.'\n});\n\nexport let tooManyRequestsError = createError({\n status: 429,\n code: 'too_many_requests',\n message: 'You have made too many requests in a short period of time.',\n hint: 'Please wait a moment and try again.'\n});\n\nexport let timeoutError = createError({\n status: 504,\n code: 'timeout',\n message: 'The request timed out.'\n});\n\nexport let methodNotAllowedError = createError({\n status: 405,\n code: 'method_not_allowed',\n message: 'The requested method is not allowed for this resource.'\n});\n"],"names":["createError","data","Object","assign","extension","_extends","object","toResponse","ok","ServiceError","_Error","error","_this","call","message","_parent","_proto","prototype","_createClass","setParent","parent","this","fromResponse","raw","key","get","_wrapNativeSuper","Error","internalServerError","status","code","badRequestError","unauthorizedError","hint","forbiddenError","invalidVersion","conflictError","goneError","paymentRequiredError","preconditionFailedError","notAcceptableError","notImplementedError","tooManyRequestsError","timeoutError","methodNotAllowedError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ACH"}
|
package/dist/index.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{Cases as e}from"@lowerdeck/case";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var o in r)({}).hasOwnProperty.call(r,o)&&(e[o]=r[o])}return e},t.apply(null,arguments)}function r(e){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},r(e)}function o(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(o=function(){return!!e})()}function n(e,t){return n=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},n(e,t)}function s(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}function a(e){var t="function"==typeof Map?new Map:void 0;return a=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,s)}function s(){return function(e,t,r){if(o())return Reflect.construct.apply(null,arguments);var s=[null];s.push.apply(s,t);var a=new(e.bind.apply(e,s));return r&&n(a,r.prototype),a}(e,arguments,r(this).constructor)}return s.prototype=Object.create(e.prototype,{constructor:{value:s,enumerable:!1,writable:!0,configurable:!0}}),n(s,e)},a(e)}var i=function(e){return Object.assign(function(r){return void 0===r&&(r={}),i(t({},e,r))},{
|
|
1
|
+
import{Cases as e}from"@lowerdeck/case";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var o in r)({}).hasOwnProperty.call(r,o)&&(e[o]=r[o])}return e},t.apply(null,arguments)}function r(e){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},r(e)}function o(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(o=function(){return!!e})()}function n(e,t){return n=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},n(e,t)}function s(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}function a(e){var t="function"==typeof Map?new Map:void 0;return a=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,s)}function s(){return function(e,t,r){if(o())return Reflect.construct.apply(null,arguments);var s=[null];s.push.apply(s,t);var a=new(e.bind.apply(e,s));return r&&n(a,r.prototype),a}(e,arguments,r(this).constructor)}return s.prototype=Object.create(e.prototype,{constructor:{value:s,enumerable:!1,writable:!0,configurable:!0}}),n(s,e)},a(e)}var i=function(e){return Object.assign(function(r){return void 0===r&&(r={}),i(t({},e,r))},{object:"error",data:e,toResponse:function(){return t({object:"error",ok:!1},e)}})},u=/*#__PURE__*/function(e){function t(t){var r;return(r=e.call(this,t.data.message)||this).error=void 0,r.object="ServiceError",r._parent=null,r.error=t,r}var r,o;o=e,(r=t).prototype=Object.create(o.prototype),r.prototype.constructor=r,n(r,o);var a,u,c=t.prototype;return c.setParent=function(e){return this._parent=e,this},c.toResponse=function(){return this.error.toResponse()},t.fromResponse=function(e){var r=e;return delete r.ok,delete r.object,new t(i(r))},a=t,(u=[{key:"parent",get:function(){return this._parent}},{key:"data",get:function(){return this.error.data}}])&&function(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,s(o.key),o)}}(a.prototype,u),Object.defineProperty(a,"prototype",{writable:!1}),a}(/*#__PURE__*/a(Error)),c=function(e){return"ServiceError"===(null==e?void 0:e.object)||"ErrorRecord"===(null==e?void 0:e.object)},d=function(e){return i(t({status:400,code:"invalid_data",message:"The provided data is invalid.",hint:"Make sure the data you are sending follows the specification outlined in the documentation."},e))},l=i({status:500,code:"internal_server_error",message:"An internal server error occurred."}),f=i({status:400,code:"bad_request",message:"The request is invalid."});function p(r,o){var n="string"==typeof r?r:r.entity,s="string"==typeof r?o:r.id;return i(t({status:404,code:"not_found",message:"The requested "+e.toKebabCase(n)+" could not be found.",hint:"Make sure the resource you are trying to access exists, that you have access to it and that it has not been deleted.",entity:n,id:null!=s?s:void 0},"string"==typeof r?{}:r))}var h=i({status:401,code:"unauthorized",message:"You are not authorized to access this resource.",hint:"Make sure you are logged in and have the correct permissions to access this resource."}),y=i({status:403,code:"forbidden",message:"You do not have permission to access this resource."}),v=i({status:400,code:"invalid_version",message:"The endpoint does not support the requested version."}),m=i({status:409,code:"conflict",message:"A similar resource already exists."}),b=i({status:410,code:"gone",message:"The requested resource is no longer available."}),g=i({status:402,code:"payment_required",message:"Payment is required to access this resource."}),_=i({status:412,code:"precondition_failed",message:"The precondition of the request failed."}),j=i({status:406,code:"not_acceptable",message:"The requested resource is not acceptable."}),O=i({status:501,code:"not_implemented",message:"The requested resource is not implemented."}),w=i({status:429,code:"too_many_requests",message:"You have made too many requests in a short period of time.",hint:"Please wait a moment and try again."}),q=i({status:504,code:"timeout",message:"The request timed out."}),P=i({status:405,code:"method_not_allowed",message:"The requested method is not allowed for this resource."});export{u as ServiceError,f as badRequestError,m as conflictError,i as createError,y as forbiddenError,b as goneError,l as internalServerError,v as invalidVersion,c as isServiceError,P as methodNotAllowedError,j as notAcceptableError,p as notFoundError,O as notImplementedError,g as paymentRequiredError,_ as preconditionFailedError,q as timeoutError,w as tooManyRequestsError,h as unauthorizedError,d as validationError};
|
|
2
2
|
//# sourceMappingURL=index.module.js.map
|
package/dist/index.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.module.js","sources":["../src/error.ts","../src/defaultErrors.ts"],"sourcesContent":["export interface ErrorData<Code extends string, Status extends number> {\n status: Status;\n code: Code;\n message: string;\n hint?: string;\n description?: string;\n reason?: string;\n [key: string]: any;\n}\n\nexport type ErrorRecord<Code extends string, Status extends number> = {\n
|
|
1
|
+
{"version":3,"file":"index.module.js","sources":["../src/error.ts","../src/defaultErrors.ts"],"sourcesContent":["export interface ErrorData<Code extends string, Status extends number> {\n status: Status;\n code: Code;\n message: string;\n hint?: string;\n description?: string;\n reason?: string;\n [key: string]: any;\n}\n\nexport type ErrorRecordExtension<Code extends string, Status extends number> = <\n NewCode extends string = Code\n>(\n extension?: Partial<ErrorData<NewCode, Status>>\n) => ErrorRecord<NewCode, Status>;\n\nexport type ErrorRecord<Code extends string, Status extends number> = {\n object: 'error';\n data: ErrorData<Code, Status>;\n toResponse: () => ErrorData<Code, Status> & { object: 'error'; ok: false };\n} & ErrorRecordExtension<Code, Status>;\n\nexport let createError = <Code extends string, Status extends number>(\n data: ErrorData<Code, Status>\n): ErrorRecord<Code, Status> => {\n return Object.assign(\n <NewCode extends string = Code>(\n extension: Partial<ErrorData<NewCode, Status>> = {}\n ): ErrorRecord<NewCode, Status> =>\n createError({\n ...data,\n ...extension\n }) as any,\n {\n object: 'error' as const,\n data,\n toResponse: () => ({ object: 'error' as const, ok: false as const, ...data })\n }\n ) as any;\n};\n\nexport class ServiceError<InnerError extends ErrorRecord<any, any>> extends Error {\n object = 'ServiceError' as const;\n\n private _parent: Error | null = null;\n\n public get parent() {\n return this._parent;\n }\n\n constructor(private readonly error: InnerError) {\n super(error.data.message);\n }\n\n setParent(parent: Error) {\n this._parent = parent;\n return this;\n }\n\n get data() {\n return this.error.data;\n }\n\n toResponse() {\n return this.error.toResponse();\n }\n\n static fromResponse(raw: ErrorData<any, any>) {\n let data = raw;\n delete data.ok;\n delete data.object;\n\n return new ServiceError(createError(data));\n }\n}\n\nexport let isServiceError = (e: any): e is ServiceError<any> => {\n return e?.object === 'ServiceError' || e?.object === 'ErrorRecord';\n};\n","import { Cases } from '@lowerdeck/case';\nimport { ValidationError } from '@lowerdeck/validation';\nimport { ErrorData, createError } from './error';\n\nexport let validationError = (\n d: { errors: ValidationError[]; entity: string } & Partial<ErrorData<'invalid_data', 400>>\n) =>\n createError({\n status: 400,\n code: 'invalid_data',\n message: 'The provided data is invalid.',\n hint: 'Make sure the data you are sending follows the specification outlined in the documentation.',\n ...d\n });\n\nexport let internalServerError = createError({\n status: 500,\n code: 'internal_server_error',\n message: 'An internal server error occurred.'\n});\n\nexport let badRequestError = createError({\n status: 400,\n code: 'bad_request',\n message: 'The request is invalid.'\n});\n\nexport function notFoundError(\n p1: { entity: string; id?: string } & Partial<ErrorData<'not_found', 404>>\n): ReturnType<typeof createError>;\nexport function notFoundError(p1: string, p2?: string | null): ReturnType<typeof createError>;\nexport function notFoundError(p1: any, p2?: any): ReturnType<typeof createError> {\n let entity = typeof p1 == 'string' ? p1 : p1.entity;\n let id = typeof p1 == 'string' ? p2 : p1.id;\n\n return createError({\n status: 404,\n code: 'not_found',\n message: `The requested ${Cases.toKebabCase(entity)} could not be found.`,\n hint: 'Make sure the resource you are trying to access exists, that you have access to it and that it has not been deleted.',\n entity,\n id: id ?? undefined,\n\n ...(typeof p1 == 'string' ? {} : p1)\n });\n}\n\nexport let unauthorizedError = createError({\n status: 401,\n code: 'unauthorized',\n message: 'You are not authorized to access this resource.',\n hint: 'Make sure you are logged in and have the correct permissions to access this resource.'\n});\n\nexport let forbiddenError = createError({\n status: 403,\n code: 'forbidden',\n message: 'You do not have permission to access this resource.'\n});\n\nexport let invalidVersion = createError({\n status: 400,\n code: 'invalid_version',\n message: 'The endpoint does not support the requested version.'\n});\n\nexport let conflictError = createError({\n status: 409,\n code: 'conflict',\n message: 'A similar resource already exists.'\n});\n\nexport let goneError = createError({\n status: 410,\n code: 'gone',\n message: 'The requested resource is no longer available.'\n});\n\nexport let paymentRequiredError = createError({\n status: 402,\n code: 'payment_required',\n message: 'Payment is required to access this resource.'\n});\n\nexport let preconditionFailedError = createError({\n status: 412,\n code: 'precondition_failed',\n message: 'The precondition of the request failed.'\n});\n\nexport let notAcceptableError = createError({\n status: 406,\n code: 'not_acceptable',\n message: 'The requested resource is not acceptable.'\n});\n\nexport let notImplementedError = createError({\n status: 501,\n code: 'not_implemented',\n message: 'The requested resource is not implemented.'\n});\n\nexport let tooManyRequestsError = createError({\n status: 429,\n code: 'too_many_requests',\n message: 'You have made too many requests in a short period of time.',\n hint: 'Please wait a moment and try again.'\n});\n\nexport let timeoutError = createError({\n status: 504,\n code: 'timeout',\n message: 'The request timed out.'\n});\n\nexport let methodNotAllowedError = createError({\n status: 405,\n code: 'method_not_allowed',\n message: 'The requested method is not allowed for this resource.'\n});\n"],"names":["createError","data","Object","assign","extension","_extends","object","toResponse","ok","ServiceError","_Error","error","_this","call","message","_parent","_proto","prototype","_createClass","setParent","parent","this","fromResponse","raw","key","get","_wrapNativeSuper","Error","isServiceError","e","validationError","d","status","code","hint","internalServerError","badRequestError","notFoundError","p1","p2","entity","id","Cases","toKebabCase","undefined","unauthorizedError","forbiddenError","invalidVersion","conflictError","goneError","paymentRequiredError","preconditionFailedError","notAcceptableError","notImplementedError","tooManyRequestsError","timeoutError","methodNotAllowedError"],"mappings":"unDAsBW,IAAAA,EAAc,SACvBC,GAEA,OAAOC,OAAOC,OACZ,SACEC,GAEA,YAFAA,IAAAA,IAAAA,EAAiD,CAAA,GAEjDJ,EAAWK,EAAA,CAAA,EACNJ,EACAG,GACI,EACX,CACEE,OAAQ,QACRL,KAAAA,EACAM,WAAY,WAAFF,OAAAA,EAAWC,CAAAA,OAAQ,QAAkBE,IAAI,GAAmBP,EAAI,GAGhF,EAEaQ,eAAuDC,SAAAA,GASlE,SAAAD,EAA6BE,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A,EAAAA,OAAMG,GAEO,iBAANN,EAAiB,CAAA,EAAKA,GAErC,CAEW,IAAAO,EAAoB7C,EAAY,CACzCgC,OAAQ,IACRC,KAAM,eACNnB,QAAS,kDACToB,KAAM,0FAGGY,EAAiB9C,EAAY,CACtCgC,OAAQ,IACRC,KAAM,YACNnB,QAAS,wDAGAiC,EAAiB/C,EAAY,CACtCgC,OAAQ,IACRC,KAAM,kBACNnB,QAAS,yDAGAkC,EAAgBhD,EAAY,CACrCgC,OAAQ,IACRC,KAAM,WACNnB,QAAS,uCAGAmC,EAAYjD,EAAY,CACjCgC,OAAQ,IACRC,KAAM,OACNnB,QAAS,mDAGAoC,EAAuBlD,EAAY,CAC5CgC,OAAQ,IACRC,KAAM,mBACNnB,QAAS,iDAGAqC,EAA0BnD,EAAY,CAC/CgC,OAAQ,IACRC,KAAM,sBACNnB,QAAS,4CAGAsC,EAAqBpD,EAAY,CAC1CgC,OAAQ,IACRC,KAAM,iBACNnB,QAAS,8CAGAuC,EAAsBrD,EAAY,CAC3CgC,OAAQ,IACRC,KAAM,kBACNnB,QAAS,+CAGAwC,EAAuBtD,EAAY,CAC5CgC,OAAQ,IACRC,KAAM,oBACNnB,QAAS,6DACToB,KAAM,wCAGGqB,EAAevD,EAAY,CACpCgC,OAAQ,IACRC,KAAM,UACNnB,QAAS,2BAGA0C,EAAwBxD,EAAY,CAC7CgC,OAAQ,IACRC,KAAM,qBACNnB,QAAS"}
|
package/dist/index.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@lowerdeck/case")):"function"==typeof define&&define.amd?define(["exports","@lowerdeck/case"],t):t((e||self).error={},e._case)}(this,function(e,t){function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var o in r)({}).hasOwnProperty.call(r,o)&&(e[o]=r[o])}return e},r.apply(null,arguments)}function o(e){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},o(e)}function n(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(n=function(){return!!e})()}function s(e,t){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},s(e,t)}function i(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}function a(e){var t="function"==typeof Map?new Map:void 0;return a=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return function(e,t,r){if(n())return Reflect.construct.apply(null,arguments);var o=[null];o.push.apply(o,t);var i=new(e.bind.apply(e,o));return r&&s(i,r.prototype),i}(e,arguments,o(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),s(r,e)},a(e)}var u=function(e){return Object.assign(function(t){return void 0===t&&(t={}),u(r({},e,t))},{
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@lowerdeck/case")):"function"==typeof define&&define.amd?define(["exports","@lowerdeck/case"],t):t((e||self).error={},e._case)}(this,function(e,t){function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var o in r)({}).hasOwnProperty.call(r,o)&&(e[o]=r[o])}return e},r.apply(null,arguments)}function o(e){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},o(e)}function n(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(n=function(){return!!e})()}function s(e,t){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},s(e,t)}function i(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}function a(e){var t="function"==typeof Map?new Map:void 0;return a=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return function(e,t,r){if(n())return Reflect.construct.apply(null,arguments);var o=[null];o.push.apply(o,t);var i=new(e.bind.apply(e,o));return r&&s(i,r.prototype),i}(e,arguments,o(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),s(r,e)},a(e)}var u=function(e){return Object.assign(function(t){return void 0===t&&(t={}),u(r({},e,t))},{object:"error",data:e,toResponse:function(){return r({object:"error",ok:!1},e)}})},c=/*#__PURE__*/function(e){function t(t){var r;return(r=e.call(this,t.data.message)||this).error=void 0,r.object="ServiceError",r._parent=null,r.error=t,r}var r,o;o=e,(r=t).prototype=Object.create(o.prototype),r.prototype.constructor=r,s(r,o);var n,a,c=t.prototype;return c.setParent=function(e){return this._parent=e,this},c.toResponse=function(){return this.error.toResponse()},t.fromResponse=function(e){var r=e;return delete r.ok,delete r.object,new t(u(r))},n=t,(a=[{key:"parent",get:function(){return this._parent}},{key:"data",get:function(){return this.error.data}}])&&function(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,i(o.key),o)}}(n.prototype,a),Object.defineProperty(n,"prototype",{writable:!1}),n}(/*#__PURE__*/a(Error)),d=u({status:500,code:"internal_server_error",message:"An internal server error occurred."}),l=u({status:400,code:"bad_request",message:"The request is invalid."}),f=u({status:401,code:"unauthorized",message:"You are not authorized to access this resource.",hint:"Make sure you are logged in and have the correct permissions to access this resource."}),p=u({status:403,code:"forbidden",message:"You do not have permission to access this resource."}),h=u({status:400,code:"invalid_version",message:"The endpoint does not support the requested version."}),y=u({status:409,code:"conflict",message:"A similar resource already exists."}),m=u({status:410,code:"gone",message:"The requested resource is no longer available."}),v=u({status:402,code:"payment_required",message:"Payment is required to access this resource."}),b=u({status:412,code:"precondition_failed",message:"The precondition of the request failed."}),g=u({status:406,code:"not_acceptable",message:"The requested resource is not acceptable."}),_=u({status:501,code:"not_implemented",message:"The requested resource is not implemented."}),E=u({status:429,code:"too_many_requests",message:"You have made too many requests in a short period of time.",hint:"Please wait a moment and try again."}),j=u({status:504,code:"timeout",message:"The request timed out."}),O=u({status:405,code:"method_not_allowed",message:"The requested method is not allowed for this resource."});e.ServiceError=c,e.badRequestError=l,e.conflictError=y,e.createError=u,e.forbiddenError=p,e.goneError=m,e.internalServerError=d,e.invalidVersion=h,e.isServiceError=function(e){return"ServiceError"===(null==e?void 0:e.object)||"ErrorRecord"===(null==e?void 0:e.object)},e.methodNotAllowedError=O,e.notAcceptableError=g,e.notFoundError=function(e,o){var n="string"==typeof e?e:e.entity,s="string"==typeof e?o:e.id;return u(r({status:404,code:"not_found",message:"The requested "+t.Cases.toKebabCase(n)+" could not be found.",hint:"Make sure the resource you are trying to access exists, that you have access to it and that it has not been deleted.",entity:n,id:null!=s?s:void 0},"string"==typeof e?{}:e))},e.notImplementedError=_,e.paymentRequiredError=v,e.preconditionFailedError=b,e.timeoutError=j,e.tooManyRequestsError=E,e.unauthorizedError=f,e.validationError=function(e){return u(r({status:400,code:"invalid_data",message:"The provided data is invalid.",hint:"Make sure the data you are sending follows the specification outlined in the documentation."},e))}});
|
|
2
2
|
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/error.ts","../src/defaultErrors.ts"],"sourcesContent":["export interface ErrorData<Code extends string, Status extends number> {\n status: Status;\n code: Code;\n message: string;\n hint?: string;\n description?: string;\n reason?: string;\n [key: string]: any;\n}\n\nexport type ErrorRecord<Code extends string, Status extends number> = {\n
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/error.ts","../src/defaultErrors.ts"],"sourcesContent":["export interface ErrorData<Code extends string, Status extends number> {\n status: Status;\n code: Code;\n message: string;\n hint?: string;\n description?: string;\n reason?: string;\n [key: string]: any;\n}\n\nexport type ErrorRecordExtension<Code extends string, Status extends number> = <\n NewCode extends string = Code\n>(\n extension?: Partial<ErrorData<NewCode, Status>>\n) => ErrorRecord<NewCode, Status>;\n\nexport type ErrorRecord<Code extends string, Status extends number> = {\n object: 'error';\n data: ErrorData<Code, Status>;\n toResponse: () => ErrorData<Code, Status> & { object: 'error'; ok: false };\n} & ErrorRecordExtension<Code, Status>;\n\nexport let createError = <Code extends string, Status extends number>(\n data: ErrorData<Code, Status>\n): ErrorRecord<Code, Status> => {\n return Object.assign(\n <NewCode extends string = Code>(\n extension: Partial<ErrorData<NewCode, Status>> = {}\n ): ErrorRecord<NewCode, Status> =>\n createError({\n ...data,\n ...extension\n }) as any,\n {\n object: 'error' as const,\n data,\n toResponse: () => ({ object: 'error' as const, ok: false as const, ...data })\n }\n ) as any;\n};\n\nexport class ServiceError<InnerError extends ErrorRecord<any, any>> extends Error {\n object = 'ServiceError' as const;\n\n private _parent: Error | null = null;\n\n public get parent() {\n return this._parent;\n }\n\n constructor(private readonly error: InnerError) {\n super(error.data.message);\n }\n\n setParent(parent: Error) {\n this._parent = parent;\n return this;\n }\n\n get data() {\n return this.error.data;\n }\n\n toResponse() {\n return this.error.toResponse();\n }\n\n static fromResponse(raw: ErrorData<any, any>) {\n let data = raw;\n delete data.ok;\n delete data.object;\n\n return new ServiceError(createError(data));\n }\n}\n\nexport let isServiceError = (e: any): e is ServiceError<any> => {\n return e?.object === 'ServiceError' || e?.object === 'ErrorRecord';\n};\n","import { Cases } from '@lowerdeck/case';\nimport { ValidationError } from '@lowerdeck/validation';\nimport { ErrorData, createError } from './error';\n\nexport let validationError = (\n d: { errors: ValidationError[]; entity: string } & Partial<ErrorData<'invalid_data', 400>>\n) =>\n createError({\n status: 400,\n code: 'invalid_data',\n message: 'The provided data is invalid.',\n hint: 'Make sure the data you are sending follows the specification outlined in the documentation.',\n ...d\n });\n\nexport let internalServerError = createError({\n status: 500,\n code: 'internal_server_error',\n message: 'An internal server error occurred.'\n});\n\nexport let badRequestError = createError({\n status: 400,\n code: 'bad_request',\n message: 'The request is invalid.'\n});\n\nexport function notFoundError(\n p1: { entity: string; id?: string } & Partial<ErrorData<'not_found', 404>>\n): ReturnType<typeof createError>;\nexport function notFoundError(p1: string, p2?: string | null): ReturnType<typeof createError>;\nexport function notFoundError(p1: any, p2?: any): ReturnType<typeof createError> {\n let entity = typeof p1 == 'string' ? p1 : p1.entity;\n let id = typeof p1 == 'string' ? p2 : p1.id;\n\n return createError({\n status: 404,\n code: 'not_found',\n message: `The requested ${Cases.toKebabCase(entity)} could not be found.`,\n hint: 'Make sure the resource you are trying to access exists, that you have access to it and that it has not been deleted.',\n entity,\n id: id ?? undefined,\n\n ...(typeof p1 == 'string' ? {} : p1)\n });\n}\n\nexport let unauthorizedError = createError({\n status: 401,\n code: 'unauthorized',\n message: 'You are not authorized to access this resource.',\n hint: 'Make sure you are logged in and have the correct permissions to access this resource.'\n});\n\nexport let forbiddenError = createError({\n status: 403,\n code: 'forbidden',\n message: 'You do not have permission to access this resource.'\n});\n\nexport let invalidVersion = createError({\n status: 400,\n code: 'invalid_version',\n message: 'The endpoint does not support the requested version.'\n});\n\nexport let conflictError = createError({\n status: 409,\n code: 'conflict',\n message: 'A similar resource already exists.'\n});\n\nexport let goneError = createError({\n status: 410,\n code: 'gone',\n message: 'The requested resource is no longer available.'\n});\n\nexport let paymentRequiredError = createError({\n status: 402,\n code: 'payment_required',\n message: 'Payment is required to access this resource.'\n});\n\nexport let preconditionFailedError = createError({\n status: 412,\n code: 'precondition_failed',\n message: 'The precondition of the request failed.'\n});\n\nexport let notAcceptableError = createError({\n status: 406,\n code: 'not_acceptable',\n message: 'The requested resource is not acceptable.'\n});\n\nexport let notImplementedError = createError({\n status: 501,\n code: 'not_implemented',\n message: 'The requested resource is not implemented.'\n});\n\nexport let tooManyRequestsError = createError({\n status: 429,\n code: 'too_many_requests',\n message: 'You have made too many requests in a short period of time.',\n hint: 'Please wait a moment and try again.'\n});\n\nexport let timeoutError = createError({\n status: 504,\n code: 'timeout',\n message: 'The request timed out.'\n});\n\nexport let methodNotAllowedError = createError({\n status: 405,\n code: 'method_not_allowed',\n message: 'The requested method is not allowed for this resource.'\n});\n"],"names":["createError","data","Object","assign","extension","_extends","object","toResponse","ok","ServiceError","_Error","error","_this","call","message","_parent","_proto","prototype","_createClass","setParent","parent","this","fromResponse","raw","key","get","_wrapNativeSuper","Error","internalServerError","status","code","badRequestError","unauthorizedError","hint","forbiddenError","invalidVersion","conflictError","goneError","paymentRequiredError","preconditionFailedError","notAcceptableError","notImplementedError","tooManyRequestsError","timeoutError","methodNotAllowedError","e","p1","p2","entity","id","Cases","toKebabCase","undefined","d"],"mappings":"q2DAsBW,IAAAA,EAAc,SACvBC,GAEA,OAAOC,OAAOC,OACZ,SACEC,GAEA,YAFAA,IAAAA,IAAAA,EAAiD,CAAA,GAEjDJ,EAAWK,EAAA,CAAA,EACNJ,EACAG,GACI,EACX,CACEE,OAAQ,QACRL,KAAAA,EACAM,WAAY,WAAFF,OAAAA,EAAWC,CAAAA,OAAQ,QAAkBE,IAAI,GAAmBP,EAAI,GAGhF,EAEaQ,eAAuDC,SAAAA,GASlE,SAAAD,EAA6BE,GAAiB,IAAAC,EAAA,OAC5CA,EAAAF,EAAAG,KAAMF,KAAAA,EAAMV,KAAKa,UAASF,MADCD,aAAAC,EAR7BN,OAAS,eAAuBM,EA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