Skip to content
Source
Sign inSign up
samwillis/neon-realtime-electric
Open History to see the last commit

error.ts

125 lines3.3 KB

1export class FetchError extends Error {
2 status: number
3 text?: string
4 json?: object
5 headers: Record<string, string>
6​
7 constructor(
8 status: number,
9 text: string | undefined,
10 json: object | undefined,
11 headers: Record<string, string>,
12 public url: string,
13 message?: string
14 ) {
15 super(
16 message ||
17 `HTTP Error ${status} at ${url}: ${text ?? JSON.stringify(json)}`
18 )
19 this.name = `FetchError`
20 this.status = status
21 this.text = text
22 this.json = json
23 this.headers = headers
24 }
25​
26 static async fromResponse(
27 response: Response,
28 url: string
29 ): Promise<FetchError> {
30 const status = response.status
31 const headers = Object.fromEntries([...response.headers.entries()])
32 let text: string | undefined = undefined
33 let json: object | undefined = undefined
34​
35 const contentType = response.headers.get(`content-type`)
36 if (!response.bodyUsed) {
37 if (contentType && contentType.includes(`application/json`)) {
38 json = (await response.json()) as object
39 } else {
40 text = await response.text()
41 }
42 }
43​
44 return new FetchError(status, text, json, headers, url)
45 }
47​
48export class FetchBackoffAbortError extends Error {
49 constructor() {
50 super(`Fetch with backoff aborted`)
51 this.name = `FetchBackoffAbortError`
52 }
54​
55export class InvalidShapeOptionsError extends Error {
56 constructor(message: string) {
57 super(message)
58 this.name = `InvalidShapeOptionsError`
59 }
61​
62export class MissingShapeUrlError extends Error {
63 constructor() {
64 super(`Invalid shape options: missing required url parameter`)
65 this.name = `MissingShapeUrlError`
66 }
68​
69export class InvalidSignalError extends Error {
70 constructor() {
71 super(`Invalid signal option. It must be an instance of AbortSignal.`)
72 this.name = `InvalidSignalError`
73 }
75​
76export class MissingShapeHandleError extends Error {
77 constructor() {
78 super(
79 `shapeHandle is required if this isn't an initial fetch (i.e. offset > -1)`
80 )
81 this.name = `MissingShapeHandleError`
82 }
84​
85export class ReservedParamError extends Error {
86 constructor(reservedParams: string[]) {
87 super(
88 `Cannot use reserved Electric parameter names in custom params: ${reservedParams.join(`, `)}`
89 )
90 this.name = `ReservedParamError`
91 }
93​
94export class ParserNullValueError extends Error {
95 constructor(columnName: string) {
96 super(`Column "${columnName ?? `unknown`}" does not allow NULL values`)
97 this.name = `ParserNullValueError`
98 }
100​
101export class ShapeStreamAlreadyRunningError extends Error {
102 constructor() {
103 super(`ShapeStream is already running`)
104 this.name = `ShapeStreamAlreadyRunningError`
105 }
107​
108export class MissingHeadersError extends Error {
109 constructor(url: string, missingHeaders: Array<string>) {
110 let msg = `The response for the shape request to ${url} didn't include the following required headers:\n`
111 missingHeaders.forEach((h) => {
112 msg += `- ${h}\n`
113 })
114 msg += `\nThis is often due to a proxy not setting CORS correctly so that all Electric headers can be read by the client.`
115 msg += `\nFor more information visit the troubleshooting guide: /docs/guides/troubleshooting/missing-headers`
116 super(msg)
117 }
119​
120export class StaleCacheError extends Error {
121 constructor(message: string) {
122 super(message)
123 this.name = `StaleCacheError`
124 }