| ... | @@ -40,7 +40,6 @@ export class PrimaryEnvironmentRequestError extends Schema.TaggedErrorClass<Prim | ... | @@ -40,7 +40,6 @@ export class PrimaryEnvironmentRequestError extends Schema.TaggedErrorClass<Prim |
| 40 | { | 40 | { |
| 41 | operation: PrimaryEnvironmentRequestOperation, | 41 | operation: PrimaryEnvironmentRequestOperation, |
| 42 | status: Schema.Number, | 42 | status: Schema.Number, |
| 43 | detail: Schema.String, | | |
| 44 | pairingLinkId: Schema.optional(Schema.String), | 43 | pairingLinkId: Schema.optional(Schema.String), |
| 45 | sessionId: Schema.optional(Schema.String), | 44 | sessionId: Schema.optional(Schema.String), |
| 46 | cause: Schema.Defect(), | 45 | cause: Schema.Defect(), |
| ... | @@ -49,17 +48,13 @@ export class PrimaryEnvironmentRequestError extends Schema.TaggedErrorClass<Prim | ... | @@ -49,17 +48,13 @@ export class PrimaryEnvironmentRequestError extends Schema.TaggedErrorClass<Prim |
| 49 | static fromCause(input: { | 48 | static fromCause(input: { |
| 50 | readonly operation: PrimaryEnvironmentRequestOperation; | 49 | readonly operation: PrimaryEnvironmentRequestOperation; |
| 51 | readonly cause: unknown; | 50 | readonly cause: unknown; |
| 52 | readonly fallbackMessage: (status: number) => string; | | |
| 53 | readonly formatDetail?: (detail: string, status: number) => string; | | |
| 54 | readonly pairingLinkId?: string; | 51 | readonly pairingLinkId?: string; |
| 55 | readonly sessionId?: string; | 52 | readonly sessionId?: string; |
| 56 | }): PrimaryEnvironmentRequestError { | 53 | }): PrimaryEnvironmentRequestError { |
| 57 | const status = readHttpApiStatus(input.cause) ?? 500; | 54 | const status = readHttpApiStatus(input.cause) ?? 500; |
| 58 | const rawDetail = readHttpApiErrorMessage(input.cause, input.fallbackMessage(status)); | | |
| 59 | return new PrimaryEnvironmentRequestError({ | 55 | return new PrimaryEnvironmentRequestError({ |
| 60 | operation: input.operation, | 56 | operation: input.operation, |
| 61 | status, | 57 | status, |
| 62 | detail: input.formatDetail?.(rawDetail, status) ?? rawDetail, | | |
| 63 | ...(input.pairingLinkId !== undefined ? { pairingLinkId: input.pairingLinkId } : {}), | 58 | ...(input.pairingLinkId !== undefined ? { pairingLinkId: input.pairingLinkId } : {}), |
| 64 | ...(input.sessionId !== undefined ? { sessionId: input.sessionId } : {}), | 59 | ...(input.sessionId !== undefined ? { sessionId: input.sessionId } : {}), |
| 65 | cause: input.cause, | 60 | cause: input.cause, |
| ... | @@ -67,11 +62,59 @@ export class PrimaryEnvironmentRequestError extends Schema.TaggedErrorClass<Prim | ... | @@ -67,11 +62,59 @@ export class PrimaryEnvironmentRequestError extends Schema.TaggedErrorClass<Prim |
| 67 | } | 62 | } |
| 68 | | 63 | |
| 69 | override get message(): string { | 64 | override get message(): string { |
| 70 | return this.detail; | 65 | return `Primary environment request failed during ${this.operation} (HTTP ${this.status}).`; |
| 71 | } | 66 | } |
| 72 | } | 67 | } |
| 73 | | 68 | |
| 74 | export const isPrimaryEnvironmentRequestError = Schema.is(PrimaryEnvironmentRequestError); | 69 | export const isPrimaryEnvironmentRequestError = Schema.is(PrimaryEnvironmentRequestError); |
| | 70 | |
| | 71 | export class PrimaryEnvironmentPairingCredentialRejectedError extends Schema.TaggedErrorClass<PrimaryEnvironmentPairingCredentialRejectedError>()( |
| | 72 | "PrimaryEnvironmentPairingCredentialRejectedError", |
| | 73 | { |
| | 74 | providedLength: Schema.Number, |
| | 75 | cause: Schema.Defect(), |
| | 76 | }, |
| | 77 | ) { |
| | 78 | override get message(): string { |
| | 79 | return "Invalid pairing token. Check the token and try again."; |
| | 80 | } |
| | 81 | } |
| | 82 | |
| | 83 | export const isPrimaryEnvironmentPairingCredentialRejectedError = Schema.is( |
| | 84 | PrimaryEnvironmentPairingCredentialRejectedError, |
| | 85 | ); |
| | 86 | |
| | 87 | export class PrimaryEnvironmentAuthSessionTimeoutError extends Schema.TaggedErrorClass<PrimaryEnvironmentAuthSessionTimeoutError>()( |
| | 88 | "PrimaryEnvironmentAuthSessionTimeoutError", |
| | 89 | { |
| | 90 | timeoutMs: Schema.Number, |
| | 91 | elapsedMs: Schema.Number, |
| | 92 | }, |
| | 93 | ) { |
| | 94 | override get message(): string { |
| | 95 | return "Timed out waiting for authenticated session after bootstrap."; |
| | 96 | } |
| | 97 | } |
| | 98 | |
| | 99 | export const isPrimaryEnvironmentAuthSessionTimeoutError = Schema.is( |
| | 100 | PrimaryEnvironmentAuthSessionTimeoutError, |
| | 101 | ); |
| | 102 | |
| | 103 | export class PrimaryEnvironmentPairingCredentialRequiredError extends Schema.TaggedErrorClass<PrimaryEnvironmentPairingCredentialRequiredError>()( |
| | 104 | "PrimaryEnvironmentPairingCredentialRequiredError", |
| | 105 | { |
| | 106 | providedLength: Schema.Number, |
| | 107 | }, |
| | 108 | ) { |
| | 109 | override get message(): string { |
| | 110 | return "Enter a pairing token to continue."; |
| | 111 | } |
| | 112 | } |
| | 113 | |
| | 114 | export const isPrimaryEnvironmentPairingCredentialRequiredError = Schema.is( |
| | 115 | PrimaryEnvironmentPairingCredentialRequiredError, |
| | 116 | ); |
| | 117 | |
| 75 | const isEnvironmentHttpCommonError = Schema.is(EnvironmentHttpCommonError); | 118 | const isEnvironmentHttpCommonError = Schema.is(EnvironmentHttpCommonError); |
| 76 | | 119 | |
| 77 | export interface ServerPairingLinkRecord { | 120 | export interface ServerPairingLinkRecord { |
| ... | @@ -151,7 +194,6 @@ export async function fetchSessionState(): Promise<AuthSessionState> { | ... | @@ -151,7 +194,6 @@ export async function fetchSessionState(): Promise<AuthSessionState> { |
| 151 | throw PrimaryEnvironmentRequestError.fromCause({ | 194 | throw PrimaryEnvironmentRequestError.fromCause({ |
| 152 | operation: "fetch-session-state", | 195 | operation: "fetch-session-state", |
| 153 | cause: error, | 196 | cause: error, |
| 154 | fallbackMessage: (status) => `Failed to load server auth session state (${status}).`, | | |
| 155 | }); | 197 | }); |
| 156 | } | 198 | } |
| 157 | }); | 199 | }); |
| ... | @@ -180,42 +222,6 @@ function readEnvironmentHttpErrorStatus(error: EnvironmentHttpCommonErrorType): | ... | @@ -180,42 +222,6 @@ function readEnvironmentHttpErrorStatus(error: EnvironmentHttpCommonErrorType): |
| 180 | } | 222 | } |
| 181 | } | 223 | } |
| 182 | | 224 | |
| 183 | function readHttpApiErrorMessage(error: unknown, fallbackMessage: string): string { | | |
| 184 | if (!isEnvironmentHttpCommonError(error)) { | | |
| 185 | return fallbackMessage; | | |
| 186 | } | | |
| 187 | switch (error._tag) { | | |
| 188 | case "EnvironmentAuthInvalidError": | | |
| 189 | return error.reason === "missing_credential" | | |
| 190 | ? "Authentication required." | | |
| 191 | : "Invalid bootstrap credential."; | | |
| 192 | case "EnvironmentRequestInvalidError": | | |
| 193 | return error.reason === "invalid_scope" | | |
| 194 | ? "Requested token scope is invalid." | | |
| 195 | : "Requested scope exceeds the bootstrap credential grant."; | | |
| 196 | case "EnvironmentScopeRequiredError": | | |
| 197 | return `The authenticated token is missing required scope: ${error.requiredScope}.`; | | |
| 198 | case "EnvironmentOperationForbiddenError": | | |
| 199 | return "This operation is not allowed for the current session."; | | |
| 200 | case "EnvironmentInternalError": | | |
| 201 | return fallbackMessage; | | |
| 202 | } | | |
| 203 | } | | |
| 204 | | | |
| 205 | const INVALID_BOOTSTRAP_CREDENTIAL_MESSAGES = new Set([ | | |
| 206 | "Invalid bootstrap credential.", | | |
| 207 | "Unknown bootstrap credential.", | | |
| 208 | ]); | | |
| 209 | | | |
| 210 | function toFriendlyBootstrapErrorMessage(status: number, message: string): string { | | |
| 211 | const trimmedMessage = message.trim(); | | |
| 212 | if (status === 401 && INVALID_BOOTSTRAP_CREDENTIAL_MESSAGES.has(trimmedMessage)) { | | |
| 213 | return "Invalid pairing token. Check the token and try again."; | | |
| 214 | } | | |
| 215 | | | |
| 216 | return trimmedMessage; | | |
| 217 | } | | |
| 218 | | | |
| 219 | async function exchangeBootstrapCredential(credential: string): Promise<AuthBrowserSessionResult> { | 225 | async function exchangeBootstrapCredential(credential: string): Promise<AuthBrowserSessionResult> { |
| 220 | return retryTransientBootstrap(async () => { | 226 | return retryTransientBootstrap(async () => { |
| 221 | try { | 227 | try { |
| ... | @@ -225,11 +231,19 @@ async function exchangeBootstrapCredential(credential: string): Promise<AuthBrow | ... | @@ -225,11 +231,19 @@ async function exchangeBootstrapCredential(credential: string): Promise<AuthBrow |
| 225 | ), | 231 | ), |
| 226 | ); | 232 | ); |
| 227 | } catch (error) { | 233 | } catch (error) { |
| | 234 | if ( |
| | 235 | isEnvironmentHttpCommonError(error) && |
| | 236 | error._tag === "EnvironmentAuthInvalidError" && |
| | 237 | error.reason === "invalid_credential" |
| | 238 | ) { |
| | 239 | throw new PrimaryEnvironmentPairingCredentialRejectedError({ |
| | 240 | providedLength: credential.length, |
| | 241 | cause: error, |
| | 242 | }); |
| | 243 | } |
| 228 | throw PrimaryEnvironmentRequestError.fromCause({ | 244 | throw PrimaryEnvironmentRequestError.fromCause({ |
| 229 | operation: "exchange-bootstrap-credential", | 245 | operation: "exchange-bootstrap-credential", |
| 230 | cause: error, | 246 | cause: error, |
| 231 | fallbackMessage: (status) => `Failed to bootstrap auth session (${status}).`, | | |
| 232 | formatDetail: (detail, status) => toFriendlyBootstrapErrorMessage(status, detail), | | |
| 233 | }); | 247 | }); |
| 234 | } | 248 | } |
| 235 | }); | 249 | }); |
| ... | @@ -244,8 +258,12 @@ async function waitForAuthenticatedSessionAfterBootstrap(): Promise<AuthSessionS | ... | @@ -244,8 +258,12 @@ async function waitForAuthenticatedSessionAfterBootstrap(): Promise<AuthSessionS |
| 244 | return session; | 258 | return session; |
| 245 | } | 259 | } |
| 246 | | 260 | |
| 247 | if (Date.now() - startedAt >= AUTH_SESSION_ESTABLISH_TIMEOUT_MS) { | 261 | const elapsedMs = Date.now() - startedAt; |
| 248 | throw new Error("Timed out waiting for authenticated session after bootstrap."); | 262 | if (elapsedMs >= AUTH_SESSION_ESTABLISH_TIMEOUT_MS) { |
| | 263 | throw new PrimaryEnvironmentAuthSessionTimeoutError({ |
| | 264 | timeoutMs: AUTH_SESSION_ESTABLISH_TIMEOUT_MS, |
| | 265 | elapsedMs, |
| | 266 | }); |
| 249 | } | 267 | } |
| 250 | | 268 | |
| 251 | await waitForBootstrapRetry(AUTH_SESSION_ESTABLISH_STEP_MS); | 269 | await waitForBootstrapRetry(AUTH_SESSION_ESTABLISH_STEP_MS); |
| ... | @@ -323,7 +341,9 @@ async function bootstrapServerAuth(): Promise<ServerAuthGateState> { | ... | @@ -323,7 +341,9 @@ async function bootstrapServerAuth(): Promise<ServerAuthGateState> { |
| 323 | export async function submitServerAuthCredential(credential: string): Promise<void> { | 341 | export async function submitServerAuthCredential(credential: string): Promise<void> { |
| 324 | const trimmedCredential = credential.trim(); | 342 | const trimmedCredential = credential.trim(); |
| 325 | if (!trimmedCredential) { | 343 | if (!trimmedCredential) { |
| 326 | throw new Error("Enter a pairing token to continue."); | 344 | throw new PrimaryEnvironmentPairingCredentialRequiredError({ |
| | 345 | providedLength: credential.length, |
| | 346 | }); |
| 327 | } | 347 | } |
| 328 | | 348 | |
| 329 | resolvedAuthenticatedGateState = null; | 349 | resolvedAuthenticatedGateState = null; |
| ... | @@ -355,7 +375,6 @@ export async function createServerPairingCredential(input?: { | ... | @@ -355,7 +375,6 @@ export async function createServerPairingCredential(input?: { |
| 355 | throw PrimaryEnvironmentRequestError.fromCause({ | 375 | throw PrimaryEnvironmentRequestError.fromCause({ |
| 356 | operation: "create-pairing-credential", | 376 | operation: "create-pairing-credential", |
| 357 | cause: error, | 377 | cause: error, |
| 358 | fallbackMessage: (status) => `Failed to create pairing credential (${status}).`, | | |
| 359 | }); | 378 | }); |
| 360 | } | 379 | } |
| 361 | } | 380 | } |
| ... | @@ -396,7 +415,6 @@ export async function listServerPairingLinks(): Promise<ReadonlyArray<ServerPair | ... | @@ -396,7 +415,6 @@ export async function listServerPairingLinks(): Promise<ReadonlyArray<ServerPair |
| 396 | throw PrimaryEnvironmentRequestError.fromCause({ | 415 | throw PrimaryEnvironmentRequestError.fromCause({ |
| 397 | operation: "list-pairing-links", | 416 | operation: "list-pairing-links", |
| 398 | cause: error, | 417 | cause: error, |
| 399 | fallbackMessage: (status) => `Failed to load pairing links (${status}).`, | | |
| 400 | }); | 418 | }); |
| 401 | } | 419 | } |
| 402 | } | 420 | } |
| ... | @@ -413,7 +431,6 @@ export async function revokeServerPairingLink(id: string): Promise<void> { | ... | @@ -413,7 +431,6 @@ export async function revokeServerPairingLink(id: string): Promise<void> { |
| 413 | operation: "revoke-pairing-link", | 431 | operation: "revoke-pairing-link", |
| 414 | pairingLinkId: id, | 432 | pairingLinkId: id, |
| 415 | cause: error, | 433 | cause: error, |
| 416 | fallbackMessage: (status) => `Failed to revoke pairing link (${status}).`, | | |
| 417 | }); | 434 | }); |
| 418 | } | 435 | } |
| 419 | } | 436 | } |
| ... | @@ -446,7 +463,6 @@ export async function listServerClientSessions(): Promise< | ... | @@ -446,7 +463,6 @@ export async function listServerClientSessions(): Promise< |
| 446 | throw PrimaryEnvironmentRequestError.fromCause({ | 463 | throw PrimaryEnvironmentRequestError.fromCause({ |
| 447 | operation: "list-client-sessions", | 464 | operation: "list-client-sessions", |
| 448 | cause: error, | 465 | cause: error, |
| 449 | fallbackMessage: (status) => `Failed to load paired clients (${status}).`, | | |
| 450 | }); | 466 | }); |
| 451 | } | 467 | } |
| 452 | } | 468 | } |
| ... | @@ -465,7 +481,6 @@ export async function revokeServerClientSession(sessionId: AuthSessionId): Promi | ... | @@ -465,7 +481,6 @@ export async function revokeServerClientSession(sessionId: AuthSessionId): Promi |
| 465 | operation: "revoke-client-session", | 481 | operation: "revoke-client-session", |
| 466 | sessionId, | 482 | sessionId, |
| 467 | cause: error, | 483 | cause: error, |
| 468 | fallbackMessage: (status) => `Failed to revoke client session (${status}).`, | | |
| 469 | }); | 484 | }); |
| 470 | } | 485 | } |
| 471 | } | 486 | } |
| ... | @@ -482,7 +497,6 @@ export async function revokeOtherServerClientSessions(): Promise<number> { | ... | @@ -482,7 +497,6 @@ export async function revokeOtherServerClientSessions(): Promise<number> { |
| 482 | throw PrimaryEnvironmentRequestError.fromCause({ | 497 | throw PrimaryEnvironmentRequestError.fromCause({ |
| 483 | operation: "revoke-other-client-sessions", | 498 | operation: "revoke-other-client-sessions", |
| 484 | cause: error, | 499 | cause: error, |
| 485 | fallbackMessage: (status) => `Failed to revoke other client sessions (${status}).`, | | |
| 486 | }); | 500 | }); |
| 487 | } | 501 | } |
| 488 | } | 502 | } |