UNPKG

@squarecloud/blob

Version:
61 lines (60 loc) 1.53 kB
// src/structures/error.ts var SquareCloudBlobError = class _SquareCloudBlobError extends Error { constructor(code, message, cause) { super(message, { cause }); this.name = _SquareCloudBlobError.name; this.message = this.getMessage(code); } getMessage(rawCode) { const code = rawCode.replaceAll("_", " ").toLowerCase().replace(/(^|\s)\S/g, (L) => L.toUpperCase()); const message = this.message ? `: ${this.message}` : ""; return `${code}${message}`; } }; var SquareCloudValidationError = class _SquareCloudValidationError extends SquareCloudBlobError { constructor(...args) { super(...args); this.name = _SquareCloudValidationError.name; } }; // src/validation/assertions/handlers.ts function handleLiteralAssertion({ schema, value, expect, code }) { try { schema.parse(value); } catch { throw new SquareCloudValidationError( code ? `INVALID_${code}` : "VALIDATION_ERROR", `Expect ${expect}, got ${typeof value}` ); } } function handleAPIObjectAssertion({ schema, value, code }) { const name = code.toLowerCase().replaceAll("_", " "); try { return schema.parse(value); } catch (err) { const cause = err.errors?.map((err2) => ({ ...err2, path: err2.path.join(" > ") })); throw new SquareCloudBlobError( `INVALID_API_${code}`, `Invalid ${name} object received from API`, { cause } ); } } export { handleAPIObjectAssertion, handleLiteralAssertion }; //# sourceMappingURL=handlers.mjs.map