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-15 08:56:11 -08:00
|
|
|
quota_exceeded: 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" };
|
|
|
|
|
}
|
2026-11-14 22:50:23 -08:00
|
|
|
|
|
|
|
|
/** Human-readable copy for a failed AWS command; never raw JSON. */
|
|
|
|
|
export function awsErrorMessage(e: AwsError): string {
|
|
|
|
|
switch (e.kind) {
|
|
|
|
|
case "sso_expired":
|
|
|
|
|
return `Your SSO session for profile ${e.profile} has expired.`;
|
|
|
|
|
case "profile_not_found":
|
|
|
|
|
return `Profile ${e.profile} was not found in your AWS config, or has no credentials.`;
|
|
|
|
|
case "keys_missing":
|
|
|
|
|
return "The access keys for this connection are missing from the keychain. Remove and re-add the connection.";
|
|
|
|
|
case "state_bucket_taken":
|
|
|
|
|
return `The state bucket name ${e.bucket} is already taken by another AWS account.`;
|
|
|
|
|
default:
|
|
|
|
|
return e.message;
|
|
|
|
|
}
|
|
|
|
|
}
|