Skip to content

Commit

Permalink
More readable server errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Sep 5, 2020
1 parent bf481f4 commit 201e5ce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 61 deletions.
54 changes: 0 additions & 54 deletions packages/experimental/src.ts/retry-provider.ts

This file was deleted.

32 changes: 25 additions & 7 deletions packages/web/src.ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

import { encode as base64Encode } from "@ethersproject/base64";
import { hexlify, isBytesLike } from "@ethersproject/bytes";
import { shallowCopy } from "@ethersproject/properties";
import { toUtf8Bytes, toUtf8String } from "@ethersproject/strings";

Expand All @@ -16,6 +17,23 @@ function staller(duration: number): Promise<void> {
});
}

function bodyify(value: any, type: string): string {
if (value == null) { return null; }

if (typeof(value) === "string") { return value; }

if (isBytesLike(value)) {
if (type && (type.split("/")[0] === "text" || type === "application/json")) {
try {
return toUtf8String(value);
} catch (error) { };
}
return hexlify(value);
}

return value;
}

// Exported Types
export type ConnectionInfo = {
url: string,
Expand Down Expand Up @@ -154,7 +172,7 @@ export function _fetchData<T = Uint8Array>(connection: string | ConnectionInfo,
timer = null;

reject(logger.makeError("timeout", Logger.errors.TIMEOUT, {
requestBody: (options.body || null),
requestBody: bodyify(options.body, flatHeaders["content-type"]),
requestMethod: options.method,
timeout: timeout,
url: url
Expand Down Expand Up @@ -208,7 +226,7 @@ export function _fetchData<T = Uint8Array>(connection: string | ConnectionInfo,
if (response == null) {
runningTimeout.cancel();
logger.throwError("missing response", Logger.errors.SERVER_ERROR, {
requestBody: (options.body || null),
requestBody: bodyify(options.body, flatHeaders["content-type"]),
requestMethod: options.method,
serverError: error,
url: url
Expand All @@ -227,8 +245,8 @@ export function _fetchData<T = Uint8Array>(connection: string | ConnectionInfo,
logger.throwError("bad response", Logger.errors.SERVER_ERROR, {
status: response.statusCode,
headers: response.headers,
body: body,
requestBody: (options.body || null),
body: bodyify(body, ((response.headers) ? response.headers["content-type"]: null)),
requestBody: bodyify(options.body, flatHeaders["content-type"]),
requestMethod: options.method,
url: url
});
Expand Down Expand Up @@ -258,9 +276,9 @@ export function _fetchData<T = Uint8Array>(connection: string | ConnectionInfo,

runningTimeout.cancel();
logger.throwError("processing response error", Logger.errors.SERVER_ERROR, {
body: body,
body: bodyify(body, ((response.headers) ? response.headers["content-type"]: null)),
error: error,
requestBody: (options.body || null),
requestBody: bodyify(options.body, flatHeaders["content-type"]),
requestMethod: options.method,
url: url
});
Expand All @@ -275,7 +293,7 @@ export function _fetchData<T = Uint8Array>(connection: string | ConnectionInfo,
}

return logger.throwError("failed response", Logger.errors.SERVER_ERROR, {
requestBody: (options.body || null),
requestBody: bodyify(options.body, flatHeaders["content-type"]),
requestMethod: options.method,
url: url
});
Expand Down

0 comments on commit 201e5ce

Please sign in to comment.