File size: 3,871 Bytes
a080db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**

 * @fileoverview The client-side error types every module throws.

 *

 * These are distinct from the protocol's `ProtocolError` (which is a wire

 * payload from the cloud). These are JS errors thrown locally so a host can

 * branch with `instanceof` instead of string-matching messages.

 *

 * See docs/issues/004-cloud-client/design.md.

 */

/** Base class so a host can catch every cloud-client error with one check. */
export class CloudClientError extends Error {
  constructor(message: string) {
    super(message);
    // Without this, `instanceof` checks fail once the code is transpiled down to
    // ES5-class semantics, since the prototype chain gets reset by `super`.
    this.name = "CloudClientError";
    Object.setPrototypeOf(this, new.target.prototype);
  }
}

/**

 * A non-2xx HTTP response from a REST call.

 *

 * `status` is the HTTP status so a caller can branch (for example a 401 triggers

 * one refresh-and-retry). `code` is the optional machine-readable error code the

 * cloud puts in the JSON body, when present, for finer branching than status

 * alone allows. `retryAfterSeconds` is the server's `Retry-After` delay in whole

 * seconds, set only when the response carried the delta-seconds form.

 */
export class HttpError extends CloudClientError {
  status!: number;
  code?: string;
  retryAfterSeconds?: number;

  constructor(message: string, status: number, code?: string, retryAfterSeconds?: number) {
    super(message);
    this.name = "HttpError";
    this.status = status;
    this.code = code;
    this.retryAfterSeconds = retryAfterSeconds;
    Object.setPrototypeOf(this, new.target.prototype);
  }
}

/**

 * Thrown by `cloud.auth.clearSession()` when local credentials were cleared but

 * Core did not confirm revocation of the session (revoke failed after bounded

 * retries, or the token needed to authorize it could not be refreshed). The

 * client is logged out locally; the host must not treat the server-side session

 * as dead and should surface or defer the failure. `cause` is the underlying error.

 */
export class SessionRevocationError extends CloudClientError {
  readonly cause: unknown;

  constructor(message: string, options?: { cause?: unknown }) {
    super(message);
    this.name = "SessionRevocationError";
    this.cause = options?.cause;
    Object.setPrototypeOf(this, new.target.prototype);
  }
}

/**

 * Thrown when a token refresh fails and the host must send the user back through

 * login. Separate from `HttpError` so a host can catch the "credentials are

 * truly dead" case on its own without inspecting status codes.

 */
export class AuthExpiredError extends CloudClientError {
  constructor(message = "Authentication expired; re-auth required") {
    super(message);
    this.name = "AuthExpiredError";
    Object.setPrototypeOf(this, new.target.prototype);
  }
}

/**

 * Thrown when a credential request could not be completed for a reason that does

 * not show the credentials are dead: the network failed or timed out, Core

 * answered 408, 429 or 5xx, or the answer was not a Core answer at all (a body

 * that is not JSON, an unrecognized 4xx). The stored credentials were kept, so

 * the same call may well succeed later; a host retries instead of re-authenticating,

 * and `onExpired` does not fire. `status` is the HTTP status when something

 * answered, and `cause` is the underlying failure.

 */
export class AuthUnavailableError extends CloudClientError {
  readonly status?: number;
  readonly cause: unknown;

  constructor(message: string, options?: { status?: number; cause?: unknown }) {
    super(message);
    this.name = "AuthUnavailableError";
    this.status = options?.status;
    this.cause = options?.cause;
    Object.setPrototypeOf(this, new.target.prototype);
  }
}