class CliError extends Error {
    exitCode;
    constructor(message, exitCode = 1) {
        super(message);
        this.exitCode = exitCode;
        this.name = "CliError";
    }
}
export class ValidationError extends CliError {
    constructor(message) {
        super(message, 2);
        this.name = "ValidationError";
    }
}
export class ApiError extends CliError {
    status;
    code;
    constructor(message, status, code) {
        super(message, 3);
        this.status = status;
        this.code = code;
        this.name = "ApiError";
    }
}
/**
 * Parse `raw` as a JSON object, throwing {@link ValidationError} when it is not
 * valid JSON or not a top-level object (arrays and primitives are rejected).
 * `label` names the source for the error messages, e.g. `"on stdin"` or
 * `` `in ${path}` ``.
 */
export function parseJsonObject(raw, label) {
    let parsed;
    try {
        parsed = JSON.parse(raw);
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        throw new ValidationError(`Invalid JSON ${label}: ${message}`);
    }
    if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
        throw new ValidationError(`JSON ${label} must be a top-level object (got array or primitive).`);
    }
    return parsed;
}
//# sourceMappingURL=errors.js.map