Skip to content

Commit

Permalink
fix: handle bad http respnse codes better
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Nov 5, 2024
1 parent f95ba72 commit 1ad7b2d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
17 changes: 13 additions & 4 deletions shared/packages/worker/src/worker/accessorHandlers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
async checkPackageReadAccess(): Promise<AccessorHandlerCheckPackageReadAccessResult> {
const header = await this.fetchHeader()

if (header.status >= 400) {
if (this.isBadHTTPResponseCode(header.status)) {
return {
success: false,
reason: {
Expand Down Expand Up @@ -149,6 +149,12 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
const fetch = fetchWithController(this.fullUrl)
const res = await fetch.response

if (this.isBadHTTPResponseCode(res.status)) {
throw new Error(
`HTTP.getPackageReadStream: Bad response: [${res.status}]: ${res.statusText}, GET ${this.fullUrl}`
)
}

return {
readStream: res.body,
cancel: () => {
Expand Down Expand Up @@ -400,7 +406,7 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
method: 'DELETE',
})
if (result.status === 404) return false // that's ok
if (result.status >= 400) {
if (this.isBadHTTPResponseCode(result.status)) {
const text = await result.text()
throw new Error(
`deletePackageIfExists: Bad response: [${result.status}]: ${result.statusText}, DELETE ${this.fullUrl}, ${text}`
Expand All @@ -422,7 +428,7 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
private async fetchJSON(url: string): Promise<any | undefined> {
const result = await fetchWithTimeout(url)
if (result.status === 404) return undefined
if (result.status >= 400) {
if (this.isBadHTTPResponseCode(result.status)) {
const text = await result.text()
throw new Error(
`getPackagesToRemove: Bad response: [${result.status}]: ${result.statusText}, GET ${url}, ${text}`
Expand All @@ -437,7 +443,7 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
method: 'POST',
body: formData,
})
if (result.status >= 400) {
if (this.isBadHTTPResponseCode(result.status)) {
const text = await result.text()
throw new Error(`storeJSON: Bad response: [${result.status}]: ${result.statusText}, POST ${url}, ${text}`)
}
Expand All @@ -449,6 +455,9 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
private _getFilePath(): string | undefined {
return this.accessor.url || this.content.path
}
private isBadHTTPResponseCode(code: number) {
return code >= 400
}
}
interface HTTPHeaders {
contentType: string | null
Expand Down
19 changes: 14 additions & 5 deletions shared/packages/worker/src/worker/accessorHandlers/httpProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class HTTPProxyAccessorHandle<Metadata> extends GenericAccessorHandle<Met
async checkPackageReadAccess(): Promise<AccessorHandlerCheckPackageReadAccessResult> {
const header = await this.fetchHeader()

if (header.status >= 400) {
if (this.isBadHTTPResponseCode(header.status)) {
return {
success: false,
reason: {
Expand Down Expand Up @@ -152,6 +152,12 @@ export class HTTPProxyAccessorHandle<Metadata> extends GenericAccessorHandle<Met
const fetch = fetchWithController(this.fullUrl)
const res = await fetch.response

if (this.isBadHTTPResponseCode(res.status)) {
throw new Error(
`HTTP.getPackageReadStream: Bad response: [${res.status}]: ${res.statusText}, GET ${this.fullUrl}`
)
}

return {
readStream: res.body,
cancel: () => {
Expand All @@ -176,7 +182,7 @@ export class HTTPProxyAccessorHandle<Metadata> extends GenericAccessorHandle<Met

fetch.response
.then((result) => {
if (result.status >= 400) {
if (this.isBadHTTPResponseCode(result.status)) {
throw new Error(
`Upload file: Bad response: [${result.status}]: ${result.statusText} POST "${this.fullUrl}"`
)
Expand Down Expand Up @@ -389,7 +395,7 @@ export class HTTPProxyAccessorHandle<Metadata> extends GenericAccessorHandle<Met
method: 'DELETE',
})
if (result.status === 404) return false // that's ok
if (result.status >= 400) {
if (this.isBadHTTPResponseCode(result.status)) {
const text = await result.text()
throw new Error(
`deletePackageIfExists: Bad response: [${result.status}]: ${result.statusText}, DELETE ${this.fullUrl}, ${text}`
Expand All @@ -411,7 +417,7 @@ export class HTTPProxyAccessorHandle<Metadata> extends GenericAccessorHandle<Met
private async fetchJSON(url: string): Promise<any | undefined> {
const result = await fetchWithTimeout(url)
if (result.status === 404) return undefined
if (result.status >= 400) {
if (this.isBadHTTPResponseCode(result.status)) {
const text = await result.text()
throw new Error(
`getPackagesToRemove: Bad response: [${result.status}]: ${result.statusText}, GET ${url}, ${text}`
Expand All @@ -426,7 +432,7 @@ export class HTTPProxyAccessorHandle<Metadata> extends GenericAccessorHandle<Met
method: 'POST',
body: formData,
})
if (result.status >= 400) {
if (this.isBadHTTPResponseCode(result.status)) {
const text = await result.text()
throw new Error(`storeJSON: Bad response: [${result.status}]: ${result.statusText}, POST ${url}, ${text}`)
}
Expand All @@ -438,6 +444,9 @@ export class HTTPProxyAccessorHandle<Metadata> extends GenericAccessorHandle<Met
private _getFilePath(): string | undefined {
return this.accessor.url || this.content.filePath
}
private isBadHTTPResponseCode(code: number) {
return code >= 400
}
}
interface HTTPHeaders {
contentType: string | null
Expand Down

0 comments on commit 1ad7b2d

Please sign in to comment.