2026-11-14 16:54:47 -08:00
|
|
|
import type { AwsError } from "./types";
|
|
|
|
|
|
2026-11-14 16:57:47 -08:00
|
|
|
// A `Record` over every kind: adding or removing a kind in `AwsError` fails to compile here.
|
|
|
|
|
const KINDS: Record<AwsError["kind"], true> = {
|
|
|
|
|
sso_expired: true,
|
|
|
|
|
profile_not_found: true,
|
|
|
|
|
keys_missing: true,
|
|
|
|
|
invalid_credentials: true,
|
|
|
|
|
access_denied: true,
|
2026-11-14 22:16:05 -08:00
|
|
|
state_bucket_taken: true,
|
2026-11-14 16:57:47 -08:00
|
|
|
other: true,
|
|
|
|
|
};
|
2026-11-14 16:54:47 -08:00
|
|
|
|
|
|
|
|
/** Narrow what an AWS command rejected with. Anything that is not a tagged `AwsError` becomes `other`. */
|
|
|
|
|
export function toAwsError(e: unknown): AwsError {
|
2026-11-14 16:57:47 -08:00
|
|
|
if (e && typeof e === "object" && "kind" in e) {
|
|
|
|
|
const kind = (e as { kind: unknown }).kind;
|
|
|
|
|
if (typeof kind === "string" && Object.prototype.hasOwnProperty.call(KINDS, kind)) {
|
|
|
|
|
return e as AwsError;
|
|
|
|
|
}
|
2026-11-14 16:54:47 -08:00
|
|
|
}
|
|
|
|
|
return { kind: "other", message: typeof e === "string" ? e : e instanceof Error ? e.message : "Unknown error" };
|
|
|
|
|
}
|