@cerbos/core
Version:
Common types used by the Cerbos client libraries
70 lines • 2.06 kB
JavaScript
import { setErrorNameAndStack } from "./internal.js";
import { Status } from "./types/external.js";
/**
* Error thrown when the Cerbos policy decision point server returns an unsuccessful response.
*/
export class NotOK extends Error {
code;
details;
/**
* Parse a JSON-serialized unsuccessful response.
*/
static fromJSON(text) {
try {
const error = JSON.parse(text);
return new NotOK(code(error), details(error));
}
catch {
return new NotOK(Status.UNKNOWN, text);
}
}
/** @internal */
constructor(
/**
* The status code returned by the Cerbos policy decision point server.
*/
code,
/**
* Additional error details.
*/
details, options) {
super(`gRPC error ${code} (${Status[code]}): ${details}`, options);
this.code = code;
this.details = details;
setErrorNameAndStack(this);
}
}
function code(error) {
if (has(error, "code") &&
typeof error.code === "number" &&
error.code in Status) {
return error.code || Status.UNKNOWN;
}
throw new Error("Error does not include expected code");
}
function details(error) {
if (has(error, "message") && typeof error.message === "string") {
return error.message;
}
throw new Error("Error does not include expected details");
}
function has(object, property) {
return !!object && Object.prototype.hasOwnProperty.call(object, property);
}
/**
* Error thrown when input fails schema validation, if the {@link Client} is configured with {@link Options.onValidationError | onValidationError} set to `"throw"`.
*/
export class ValidationFailed extends Error {
validationErrors;
/** @internal */
constructor(
/**
* The validation errors that occurred.
*/
validationErrors) {
super("Input failed schema validation");
this.validationErrors = validationErrors;
setErrorNameAndStack(this);
}
}
//# sourceMappingURL=errors.js.map