From 201e5ced9c38da2de1dd7518ffbf24284d477e80 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Sat, 5 Sep 2020 04:29:08 -0400 Subject: [PATCH] More readable server errors. --- .../experimental/src.ts/retry-provider.ts | 54 ------------------- packages/web/src.ts/index.ts | 32 ++++++++--- 2 files changed, 25 insertions(+), 61 deletions(-) delete mode 100644 packages/experimental/src.ts/retry-provider.ts diff --git a/packages/experimental/src.ts/retry-provider.ts b/packages/experimental/src.ts/retry-provider.ts deleted file mode 100644 index c5208bb400..0000000000 --- a/packages/experimental/src.ts/retry-provider.ts +++ /dev/null @@ -1,54 +0,0 @@ -"use strict"; - -// RetryProvider -// -// Wraps an existing Provider to provide retry logic. -// -// See: https://github.com/ethers-io/ethers.js/issues/427 - - -import { ethers } from "ethers"; -import { poll } from "@ethersproject/web"; - -import { version } from "./_version"; - -const logger = new ethers.utils.Logger(version); - -export type RetryOptions = { - // Maximum time in total to retry - timeout?: number, - - // Minimum Duration to wait between retries - floor?: number, - - // Maximum Duration to wait between retries - ceiling?: number, - - // The slot interval for exponential back-off - interval?: number, - - // Maximum number of times to rety - retryLimit?: number -}; - -export class RetryProvider extends ethers.providers.BaseProvider { - readonly provider: ethers.providers.BaseProvider; - readonly options: RetryOptions; - - constructor(provider: ethers.providers.BaseProvider, options?: RetryOptions) { - logger.checkNew(new.target, RetryProvider); - super(provider.getNetwork()); - ethers.utils.defineReadOnly(this, "provider", provider); - ethers.utils.defineReadOnly(this, "options", options || { }); - } - - perform(method: string, params: any): Promise { - return poll(() => { - return this.provider.perform(method, params).then((result) => { - return result - }, (error) => { - return undefined - }); - }, this.options); - } -} diff --git a/packages/web/src.ts/index.ts b/packages/web/src.ts/index.ts index 0445cbf378..dd6eb83ca8 100644 --- a/packages/web/src.ts/index.ts +++ b/packages/web/src.ts/index.ts @@ -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"; @@ -16,6 +17,23 @@ function staller(duration: number): Promise { }); } +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, @@ -154,7 +172,7 @@ export function _fetchData(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 @@ -208,7 +226,7 @@ export function _fetchData(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 @@ -227,8 +245,8 @@ export function _fetchData(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 }); @@ -258,9 +276,9 @@ export function _fetchData(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 }); @@ -275,7 +293,7 @@ export function _fetchData(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 });