Skip to content

Commit

Permalink
Merge pull request #129 from AuHau/feat/better_errors
Browse files Browse the repository at this point in the history
feat: expose error messages
  • Loading branch information
Paul Le Cam authored Dec 5, 2019
2 parents 14b243b + 024ca3b commit 7319017
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
18 changes: 12 additions & 6 deletions __tests__/api-bzz-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,22 @@ describe('api-bzz-base', () => {
expect(error.status).toBe(404)
})

it('exports resOrError() utility function', () => {
it('exports resOrError() utility function', async () => {
const resOK = { ok: true }
expect(resOrError(resOK)).toBe(resOK)
expect(await resOrError(resOK)).toBe(resOK)

try {
resOrError({
await resOrError({
ok: false,
status: 400,
text: (): Promise<string> =>
Promise.resolve('Message: Some error message'),
statusText: 'Bad request',
})
} catch (error) {
expect(error instanceof HTTPError).toBe(true)
expect(error.status).toBe(400)
expect(error.message).toBe('Bad request')
expect(error.message).toBe('Some error message')
}
})

Expand All @@ -84,12 +86,14 @@ describe('api-bzz-base', () => {
await resJSON({
ok: false,
status: 400,
text: (): Promise<string> =>
Promise.resolve('Message: Some error message'),
statusText: 'Bad request',
})
} catch (error) {
expect(error instanceof HTTPError).toBe(true)
expect(error.status).toBe(400)
expect(error.message).toBe('Bad request')
expect(error.message).toBe('Some error message')
}
})

Expand All @@ -104,12 +108,14 @@ describe('api-bzz-base', () => {
await resText({
ok: false,
status: 400,
text: (): Promise<string> =>
Promise.resolve('Message: Some error message'),
statusText: 'Bad request',
})
} catch (error) {
expect(error instanceof HTTPError).toBe(true)
expect(error.status).toBe(400)
expect(error.message).toBe('Bad request')
expect(error.message).toBe('Some error message')
}
})

Expand Down
4 changes: 2 additions & 2 deletions __tests__/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ describe('browser', () => {
try {
await client.bzz.download('abcdef123456')
} catch (err) {
return err.message
return err.status
}
})
expect(errMessage).toBe('Not Found')
expect(errMessage).toBe(404)
})

it('uploads/downloads the file using bzz', async () => {
Expand Down
27 changes: 17 additions & 10 deletions packages/api-bzz-base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,34 @@ export class HTTPError extends Error {
}
}

export function resOrError<R extends BaseResponse>(res: R): R {
export async function resOrError<R extends BaseResponse>(res: R): Promise<R> {
if (res.ok) {
return res
}

throw new HTTPError(res.status, res.statusText)
const messageMatches = /Message: (.*)$/m.exec(await res.text())
if (messageMatches && messageMatches.length === 2) {
throw new HTTPError(res.status, messageMatches[1])
} else {
throw new HTTPError(res.status, res.statusText)
}
}

export async function resJSON<R extends BaseResponse, T = any>(
res: R,
): Promise<T> {
return await resOrError(res).json<T>()
return (await resOrError(res)).json<T>()
}

export function resStream<R extends BaseResponse<stream.Readable>, T = any>(
res: R,
): stream.Readable | ReadableStream {
return resOrError(res).body
export async function resStream<
R extends BaseResponse<stream.Readable>,
T = any
>(res: R): Promise<stream.Readable | ReadableStream> {
return (await resOrError(res)).body
}

export async function resText<R extends BaseResponse>(res: R): Promise<string> {
return await resOrError(res).text()
return (await resOrError(res)).text()
}

export async function resHex<R extends BaseResponse>(
Expand All @@ -101,7 +107,8 @@ export async function resHex<R extends BaseResponse>(
export async function resSwarmHash<R extends BaseResponse>(
res: R,
): Promise<string> {
const value = await resOrError(res).arrayBuffer()
const resolvedRes = await resOrError(res)
const value = await resolvedRes.arrayBuffer()
return Buffer.from(new Uint8Array(value)).toString('hex')
}

Expand Down Expand Up @@ -330,7 +337,7 @@ export class BaseBzz<
): Promise<Readable> {
const url = this.getDownloadURL(hash, options)
const res = await this.fetchTimeout(url, options)
return this.normalizeStream(resStream(res))
return this.normalizeStream(await resStream(res))
}

protected async downloadTar(
Expand Down
4 changes: 2 additions & 2 deletions packages/api-bzz-base/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export declare class HTTPError extends Error {
status: number;
constructor(status: number, message: string);
}
export declare function resOrError<R extends BaseResponse>(res: R): R;
export declare function resOrError<R extends BaseResponse>(res: R): Promise<R>;
export declare function resJSON<R extends BaseResponse, T = any>(res: R): Promise<T>;
export declare function resStream<R extends BaseResponse<stream.Readable>, T = any>(res: R): stream.Readable | ReadableStream;
export declare function resStream<R extends BaseResponse<stream.Readable>, T = any>(res: R): Promise<stream.Readable | ReadableStream>;
export declare function resText<R extends BaseResponse>(res: R): Promise<string>;
export declare function resHex<R extends BaseResponse>(res: R): Promise<hexValue>;
export declare function resSwarmHash<R extends BaseResponse>(res: R): Promise<string>;
Expand Down

0 comments on commit 7319017

Please sign in to comment.