From 1ad7b2dd6b0828c49b54cc4613ddd0ae92b9fddc Mon Sep 17 00:00:00 2001 From: Johan Nyman Date: Tue, 5 Nov 2024 13:57:42 +0100 Subject: [PATCH] fix: handle bad http respnse codes better --- .../src/worker/accessorHandlers/http.ts | 17 +++++++++++++---- .../src/worker/accessorHandlers/httpProxy.ts | 19 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/shared/packages/worker/src/worker/accessorHandlers/http.ts b/shared/packages/worker/src/worker/accessorHandlers/http.ts index 8bda82a2..bb1b2d12 100644 --- a/shared/packages/worker/src/worker/accessorHandlers/http.ts +++ b/shared/packages/worker/src/worker/accessorHandlers/http.ts @@ -104,7 +104,7 @@ export class HTTPAccessorHandle extends GenericAccessorHandle { const header = await this.fetchHeader() - if (header.status >= 400) { + if (this.isBadHTTPResponseCode(header.status)) { return { success: false, reason: { @@ -149,6 +149,12 @@ export class HTTPAccessorHandle extends GenericAccessorHandle { @@ -400,7 +406,7 @@ export class HTTPAccessorHandle extends GenericAccessorHandle= 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}` @@ -422,7 +428,7 @@ export class HTTPAccessorHandle extends GenericAccessorHandle { 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}` @@ -437,7 +443,7 @@ export class HTTPAccessorHandle extends GenericAccessorHandle= 400) { + if (this.isBadHTTPResponseCode(result.status)) { const text = await result.text() throw new Error(`storeJSON: Bad response: [${result.status}]: ${result.statusText}, POST ${url}, ${text}`) } @@ -449,6 +455,9 @@ export class HTTPAccessorHandle extends GenericAccessorHandle= 400 + } } interface HTTPHeaders { contentType: string | null diff --git a/shared/packages/worker/src/worker/accessorHandlers/httpProxy.ts b/shared/packages/worker/src/worker/accessorHandlers/httpProxy.ts index 21d5544b..8c2d44e8 100644 --- a/shared/packages/worker/src/worker/accessorHandlers/httpProxy.ts +++ b/shared/packages/worker/src/worker/accessorHandlers/httpProxy.ts @@ -109,7 +109,7 @@ export class HTTPProxyAccessorHandle extends GenericAccessorHandle { const header = await this.fetchHeader() - if (header.status >= 400) { + if (this.isBadHTTPResponseCode(header.status)) { return { success: false, reason: { @@ -152,6 +152,12 @@ export class HTTPProxyAccessorHandle extends GenericAccessorHandle { @@ -176,7 +182,7 @@ export class HTTPProxyAccessorHandle extends GenericAccessorHandle { - if (result.status >= 400) { + if (this.isBadHTTPResponseCode(result.status)) { throw new Error( `Upload file: Bad response: [${result.status}]: ${result.statusText} POST "${this.fullUrl}"` ) @@ -389,7 +395,7 @@ export class HTTPProxyAccessorHandle extends GenericAccessorHandle= 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}` @@ -411,7 +417,7 @@ export class HTTPProxyAccessorHandle extends GenericAccessorHandle { 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}` @@ -426,7 +432,7 @@ export class HTTPProxyAccessorHandle extends GenericAccessorHandle= 400) { + if (this.isBadHTTPResponseCode(result.status)) { const text = await result.text() throw new Error(`storeJSON: Bad response: [${result.status}]: ${result.statusText}, POST ${url}, ${text}`) } @@ -438,6 +444,9 @@ export class HTTPProxyAccessorHandle extends GenericAccessorHandle= 400 + } } interface HTTPHeaders { contentType: string | null