@cerbos/core
Version:
Common types used by the Cerbos client libraries
38 lines • 1.22 kB
JavaScript
import { Status } from "../types/external.js";
import { NotOK } from "./external.js";
import { setErrorNameAndStack } from "./internal.js";
/** @internal */
export class AbstractErrorResponse extends Error {
code;
constructor(code, message) {
super(message);
setErrorNameAndStack(this);
this.code = isStatus(code) && code !== Status.OK ? code : Status.UNKNOWN;
}
toNotOK(registry) {
if (registry) {
try {
for (const { typeUrl, value } of this.details()) {
const converter = registry.get(typeUrl);
if (!converter) {
continue;
}
try {
return converter.convert(this.code, this.message, this.parseDetails(converter.schema, value));
}
catch {
continue;
}
}
}
catch {
// Ignore failure to parse details
}
}
return new NotOK(this.code, this.message);
}
}
function isStatus(code) {
return code in Status;
}
//# sourceMappingURL=response.js.map