Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle bad http respnse codes better (SOFIE-3616) #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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