Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose error messages #129

Merged
merged 1 commit into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
PaulLeCam marked this conversation as resolved.
Show resolved Hide resolved
}
})
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