| 1 |
/* eslint-disable max-classes-per-file */ |
| 2 |
// This is a subset of, and the counterpart to errors.go |
| 3 |
|
| 4 |
const maybeToJson = async (response) => { |
| 5 |
try { |
| 6 |
return await response.json(); |
| 7 |
} catch (e) { |
| 8 |
return null; |
| 9 |
} |
| 10 |
}; |
| 11 |
|
| 12 |
export class UnauthorizedError extends Error { |
| 13 |
constructor() { |
| 14 |
super("Unauthorized"); |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
export class UserExistsError extends Error { |
| 19 |
static CODE = 40901; // errHTTPConflictUserExists |
| 20 |
|
| 21 |
constructor() { |
| 22 |
super("Username already exists"); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
export class TopicReservedError extends Error { |
| 27 |
static CODE = 40902; // errHTTPConflictTopicReserved |
| 28 |
|
| 29 |
constructor() { |
| 30 |
super("Topic already reserved"); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
export class AccountActionLimitReachedError extends Error { |
| 35 |
static CODE = 42906; // errHTTPTooManyRequestsLimitAccountActions |
| 36 |
|
| 37 |
constructor() { |
| 38 |
super("Account action limit reached"); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
export class IncorrectPasswordError extends Error { |
| 43 |
static CODE = 40026; // errHTTPBadRequestIncorrectPasswordConfirmation |
| 44 |
|
| 45 |
constructor() { |
| 46 |
super("Password incorrect"); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
export class EmailVerificationCodeInvalidError extends Error { |
| 51 |
static CODE = 40051; // errHTTPBadRequestEmailVerificationCodeInvalid |
| 52 |
|
| 53 |
constructor() { |
| 54 |
super("Email verification link invalid or expired"); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
export class EmailPrimaryElsewhereError extends Error { |
| 59 |
static CODE = 40908; // errHTTPConflictEmailPrimaryElsewhere |
| 60 |
|
| 61 |
constructor() { |
| 62 |
super("Email address is the recovery email on another account"); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
export const throwAppError = async (response) => { |
| 67 |
if (response.status === 401 || response.status === 403) { |
| 68 |
console.log(`[Error] HTTP ${response.status}`, response); |
| 69 |
throw new UnauthorizedError(); |
| 70 |
} |
| 71 |
const error = await maybeToJson(response); |
| 72 |
if (error?.code) { |
| 73 |
console.log(`[Error] HTTP ${response.status}, ntfy error ${error.code}: ${error.error || ""}`, response); |
| 74 |
if (error.code === UserExistsError.CODE) { |
| 75 |
throw new UserExistsError(); |
| 76 |
} else if (error.code === TopicReservedError.CODE) { |
| 77 |
throw new TopicReservedError(); |
| 78 |
} else if (error.code === AccountActionLimitReachedError.CODE) { |
| 79 |
throw new AccountActionLimitReachedError(); |
| 80 |
} else if (error.code === IncorrectPasswordError.CODE) { |
| 81 |
throw new IncorrectPasswordError(); |
| 82 |
} else if (error.code === EmailVerificationCodeInvalidError.CODE) { |
| 83 |
throw new EmailVerificationCodeInvalidError(); |
| 84 |
} else if (error.code === EmailPrimaryElsewhereError.CODE) { |
| 85 |
throw new EmailPrimaryElsewhereError(); |
| 86 |
} else if (error?.error) { |
| 87 |
throw new Error(`Error ${error.code}: ${error.error}`); |
| 88 |
} |
| 89 |
} |
| 90 |
console.log(`[Error] HTTP ${response.status}, not a ntfy error`, response); |
| 91 |
throw new Error(`Unexpected response ${response.status}`); |
| 92 |
}; |
| 93 |
|
| 94 |
export const fetchOrThrow = async (url, options) => { |
| 95 |
const response = await fetch(url, options); |
| 96 |
if (response.status !== 200) { |
| 97 |
await throwAppError(response); |
| 98 |
} |
| 99 |
return response; // Promise! |
| 100 |
}; |