Skip to content

Commit

Permalink
feat: re-introduce BeeResponseError (#877)
Browse files Browse the repository at this point in the history
* feat: re-introduce BeeResponseError

* refactor: add more properties

* ci: change node versions
  • Loading branch information
Cafe137 authored Nov 28, 2023
1 parent 85c67eb commit d29d5c4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 49 deletions.
19 changes: 2 additions & 17 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [18.x, 20.x]

steps:
- name: Checkout
Expand All @@ -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
30 changes: 1 addition & 29 deletions src/utils/data.ts
Original file line number Diff line number Diff line change
@@ -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<Blob | ReadableStream<Uint8Array> | 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<Uint8Array>

if (isReadableStream(data)) {
return readableWebToNode(data) as unknown as ReadableStream<Uint8Array>
}

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)
Expand Down
16 changes: 16 additions & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AxiosRequestConfig, AxiosResponse } from 'axios'

export class BeeError extends Error {
public constructor(message: string) {
super(message)
Expand All @@ -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,

Check warning on line 22 in src/utils/error.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Unexpected any. Specify a different type
public response?: AxiosResponse,
) {
super(message)
}
}
9 changes: 6 additions & 3 deletions src/utils/http.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -22,7 +22,10 @@ export async function http<T>(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
}
}
Expand Down

0 comments on commit d29d5c4

Please sign in to comment.