Skip to content

Commit

Permalink
feat: re-introduce BeeResponseError
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 committed Oct 31, 2023
1 parent 8ee9ed2 commit 4e08aac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
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
14 changes: 14 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,15 @@ export class BeeArgumentError extends BeeError {
super(message)
}
}

export class BeeResponseError extends BeeError {
public constructor(
message: string,
public status?: number,
public config?: AxiosRequestConfig,
public request?: any,

Check warning on line 20 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 = {
headers: {
Expand All @@ -20,7 +20,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.response?.status, e.config, e.request, e.response)
}
throw e
}
}
Expand Down

0 comments on commit 4e08aac

Please sign in to comment.