Skip to content

Commit

Permalink
feat: pretty print rpc LibraryError params, make env.DEBUG usefull
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Dec 5, 2023
1 parent ec5a5ef commit cca723f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
32 changes: 28 additions & 4 deletions __tests__/config/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,51 @@ import 'isomorphic-fetch';
/* eslint-disable no-console */
import { register } from 'fetch-intercept';

const util = require('util');

jest.setTimeout(50 * 60 * 1000);

const combiner: object[] = [];

if (process.env.DEBUG === 'true') {
register({
request(url, config) {
console.log('[fetch.request]', [url, config]);
const body = JSON.parse(config.body);
combiner.push({
request: {
url,
method: config.method,
body,
},
});
return [url, config];
},

requestError(error) {
console.log('[fetch.requestError]', error);
const match: any = combiner.find((it: any) => typeof it.result === 'undefined');
match.result = error;
console.log('[fetch.requestError]', match);
return Promise.reject(error);
},

response(response) {
console.log('[fetch.response]', response);
const cloned = response.clone();
cloned.json().then(({ result }) => {
const match: any = combiner.find((it: any) => typeof it.result === 'undefined');
if (match && 'request' in match) {
match.result = result;
console.log(util.inspect(match, false, null, true /* enable colors */));
} else {
console.log(result);
}
});
return response;
},

responseError(error) {
console.log('[fetch.responseError]', error);
const match: any = combiner.find((it: any) => typeof it.result === 'undefined');
match.result = error;
console.log('[fetch.responseError]', match);
return Promise.reject(error);
},
});
Expand Down
3 changes: 2 additions & 1 deletion src/channel/rpc_0_6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ export class RpcChannel {
if (rpcError) {
const { code, message, data } = rpcError;
throw new LibraryError(
`RPC: ${method} with params ${stringify(params)}\n ${code}: ${message}: ${stringify(data)}`
`RPC: ${method} with params ${stringify(params, null, 2)}\n
${code}: ${message}: ${stringify(data)}`
);
}
if (otherError instanceof LibraryError) {
Expand Down

0 comments on commit cca723f

Please sign in to comment.