From d29d5c441a888c8ee2881c9b085dd1072d8e340f Mon Sep 17 00:00:00 2001 From: Cafe137 <77121044+Cafe137@users.noreply.github.com> Date: Tue, 28 Nov 2023 14:05:11 +0100 Subject: [PATCH] feat: re-introduce BeeResponseError (#877) * feat: re-introduce BeeResponseError * refactor: add more properties * ci: change node versions --- .github/workflows/tests.yaml | 19 ++----------------- src/utils/data.ts | 30 +----------------------------- src/utils/error.ts | 16 ++++++++++++++++ src/utils/http.ts | 9 ++++++--- 4 files changed, 25 insertions(+), 49 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 182afa41..914aa16d 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -21,7 +21,7 @@ jobs: strategy: matrix: - node-version: [14.x, 16.x, 18.x] + node-version: [18.x, 20.x] steps: - name: Checkout @@ -40,22 +40,7 @@ jobs: # Start Bee Factory environment - name: Start Bee Factory environment - if: matrix.node-version != '18.x' run: npm run bee -- --verbose --detach - # Start Bee Factory environment for Node 18 - - name: Start Bee Factory environment for Node 18 - if: matrix.node-version == '18.x' - env: - NODE_OPTIONS: '--no-experimental-fetch' - run: npm run bee -- --verbose --detach - - - name: Tests Node 18 - if: matrix.node-version == '18.x' - env: - NODE_OPTIONS: '--no-experimental-fetch' - run: npm run test - - - name: Tests non-Node 18 - if: matrix.node-version != '18.x' + - name: Tests run: npm run test diff --git a/src/utils/data.ts b/src/utils/data.ts index 12d44882..1e214813 100644 --- a/src/utils/data.ts +++ b/src/utils/data.ts @@ -1,33 +1,5 @@ -import type { Data } from 'ws' import BlobPolyfill from 'fetch-blob' -import { isNodeReadable, isReadableStream, readableWebToNode } from './stream' -import { Readable } from '../types' - -/** - * Prepare data for valid input for node-fetch. - * - * node-fetch is not using WHATWG ReadableStream but NodeJS Readable so we need to convert in case of ReadableStream, - * but the typings are set to use ReadableStream so hence why type conversion here. - * - * @param data any string, ArrayBuffer, Uint8Array or Readable - */ -export async function prepareData( - data: string | ArrayBuffer | Uint8Array | Readable, -): Promise | never> { - if (typeof data === 'string') return new BlobPolyfill([data], { type: 'text/plain' }) as unknown as Blob - - if (data instanceof Uint8Array || data instanceof ArrayBuffer) { - return new BlobPolyfill([data], { type: 'application/octet-stream' }) as unknown as Blob - } - - if (data instanceof BlobPolyfill || isNodeReadable(data)) return data as ReadableStream - - if (isReadableStream(data)) { - return readableWebToNode(data) as unknown as ReadableStream - } - - throw new TypeError('unknown data type') -} +import type { Data } from 'ws' function isBufferArray(buffer: unknown): buffer is Buffer[] { return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer) diff --git a/src/utils/error.ts b/src/utils/error.ts index 1f6edf87..4f25580f 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -1,3 +1,5 @@ +import { AxiosRequestConfig, AxiosResponse } from 'axios' + export class BeeError extends Error { public constructor(message: string) { super(message) @@ -9,3 +11,17 @@ export class BeeArgumentError extends BeeError { super(message) } } + +export class BeeResponseError extends BeeError { + public constructor( + message: string, + public code?: string, + public axiosStatus?: string, + public status?: number, + public config?: AxiosRequestConfig, + public request?: any, + public response?: AxiosResponse, + ) { + super(message) + } +} diff --git a/src/utils/http.ts b/src/utils/http.ts index b57b6474..7bc1fa86 100644 --- a/src/utils/http.ts +++ b/src/utils/http.ts @@ -1,6 +1,6 @@ -import axios, { AxiosRequestConfig, AxiosResponse } from 'axios' +import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios' import { Objects, Strings } from 'cafe-utility' -import { BeeRequestOptions } from '../index' +import { BeeRequestOptions, BeeResponseError } from '../index' export const DEFAULT_HTTP_CONFIG: AxiosRequestConfig = { headers: { @@ -22,7 +22,10 @@ export async function http(options: BeeRequestOptions, config: AxiosRequestCo const response = await axios(requestConfig) return response - } catch (e) { + } catch (e: unknown) { + if (e instanceof AxiosError) { + throw new BeeResponseError(e.message, e.code, e.status, e.response?.status, e.config, e.request, e.response) + } throw e } }