Skip to content

Commit

Permalink
fix issue supabase#20:
Browse files Browse the repository at this point in the history
invoke() call throws if shouldThrowOnError flag is set
  • Loading branch information
vejja committed Jul 1, 2022
1 parent f22b944 commit 2bc211a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
import { resolveFetch } from './helper'
import { Fetch, FunctionInvokeOptions } from './types'

export class HttpError extends Error {
statusCode: number
statusText: string
data: any
constructor(statusCode: number, statusText: string, data: any) {
super('Invoke call returned HTTP Error code')
this.statusCode = statusCode
this.statusText = statusText
this.data = data
}
}

export class FunctionsClient {
protected url: string
protected headers: Record<string, string>
protected fetch: Fetch
protected shouldThrowOnError: boolean

constructor(
url: string,
{
headers = {},
customFetch,
shouldThrowOnError = false,
}: {
headers?: Record<string, string>
customFetch?: Fetch
shouldThrowOnError?: boolean
} = {}
) {
this.url = url
this.headers = headers
this.fetch = resolveFetch(customFetch)
this.shouldThrowOnError = shouldThrowOnError
}

/**
Expand Down Expand Up @@ -51,7 +67,7 @@ export class FunctionsClient {

const isRelayError = response.headers.get('x-relay-error')
if (isRelayError && isRelayError === 'true') {
return { data: null, error: new Error(await response.text()) }
throw new Error(await response.text())
}

let data
Expand All @@ -66,8 +82,16 @@ export class FunctionsClient {
data = await response.text()
}

// Detect HTTP status codes other than 2xx and reject as error together with statusCode property
if (!response.ok) {
throw new HttpError(response.status, response.statusText, data)
}

return { data, error: null }
} catch (error: any) {
if (this.shouldThrowOnError) {
throw error
}
return { data: null, error }
}
}
Expand Down

0 comments on commit 2bc211a

Please sign in to comment.