56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
|
|
/**
|
||
|
|
* Structured ACME protocol error with RFC 8555 fields.
|
||
|
|
* Provides type URN, subproblems, Retry-After, and retryability classification.
|
||
|
|
*/
|
||
|
|
export class AcmeError extends Error {
|
||
|
|
public readonly status: number;
|
||
|
|
public readonly type: string;
|
||
|
|
public readonly detail: string;
|
||
|
|
public readonly subproblems: Array<{ type: string; detail: string; identifier?: { type: string; value: string } }>;
|
||
|
|
public readonly url: string;
|
||
|
|
public readonly retryAfter: number;
|
||
|
|
|
||
|
|
constructor(options: {
|
||
|
|
message?: string;
|
||
|
|
status: number;
|
||
|
|
type?: string;
|
||
|
|
detail?: string;
|
||
|
|
subproblems?: Array<{ type: string; detail: string; identifier?: { type: string; value: string } }>;
|
||
|
|
url?: string;
|
||
|
|
retryAfter?: number;
|
||
|
|
}) {
|
||
|
|
const type = options.type || '';
|
||
|
|
const detail = options.detail || '';
|
||
|
|
const url = options.url || '';
|
||
|
|
const msg =
|
||
|
|
options.message ||
|
||
|
|
`ACME error: ${type || 'unknown'} (HTTP ${options.status}) at ${url || 'unknown'} - ${detail || 'no detail'}`;
|
||
|
|
super(msg);
|
||
|
|
this.name = 'AcmeError';
|
||
|
|
this.status = options.status;
|
||
|
|
this.type = type;
|
||
|
|
this.detail = detail;
|
||
|
|
this.subproblems = options.subproblems || [];
|
||
|
|
this.url = url;
|
||
|
|
this.retryAfter = options.retryAfter || 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* True for HTTP 429 or ACME rateLimited type URN
|
||
|
|
*/
|
||
|
|
get isRateLimited(): boolean {
|
||
|
|
return this.status === 429 || this.type === 'urn:ietf:params:acme:error:rateLimited';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* True for transient/retryable errors: 429, 503, 5xx, badNonce.
|
||
|
|
* False for definitive client errors: 400 (non-badNonce), 403, 404, 409.
|
||
|
|
*/
|
||
|
|
get isRetryable(): boolean {
|
||
|
|
if (this.type === 'urn:ietf:params:acme:error:badNonce') return true;
|
||
|
|
if (this.status === 429 || this.status === 503) return true;
|
||
|
|
if (this.status >= 500) return true;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|