Skip to content

Conversation

@goce-h4
Copy link
Contributor

@goce-h4 goce-h4 commented Mar 24, 2020

JSON RPC Client implementation

goce-h4 added 2 commits March 24, 2020 16:29
- added JSONRPCClient class
- added new service for testing the JSONRPCClient
Merge branch 'master' into goce/H4C-247_јson-rpc-client

# Conflicts:
#	src/index.ts
@goce-h4 goce-h4 requested a review from spion March 24, 2020 16:27
return axios
.post(url, { method: namespace + "." + prop.toString(), params, jsonrpc: "2.0" })
.then(res => Promise.resolve(res));
.then(res => Promise.resolve(res))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit is redundant

method: namespace + "." + prop.toString(),
params,
jsonrpc: "2.0",
id: ++id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only increment the counter locally. this.counter will not be affected. We need to increment the counter itself

}
}
};
return service.testError({ data: "hi" }).then(res => {
Copy link
Contributor

@spion-h4 spion-h4 Mar 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client should automatically unwrap the content, recreating and rethrowing the error. You should not be aware of any json-rpc implementation detail here.

const { code, message, data } = err.response.data;
let resp = { code, message, data };

return { jsonrpc, error: resp, id };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should recreate the CodedRpcException and throw it here.

Copy link
Contributor

@spion-h4 spion-h4 Mar 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @alantreadway-hfour whether we want to deal with different errors in a different way here.

One option is to just rethrow any error type. The other is to differentiate between client failures and server failures.

I personally think rethrowing is just fine for the basic client, then other clients can be built on top of it that catch errors and decide what to do with them based on the error code and error message (maybe retry if idempotent request and if the code indicates a retry might work out - otherwise rethrow)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's start simple, we can add more advanced (retry) behaviours later? So rethrow seems fine here to me.

- throw error in rpc client

it(`should throw an error on /rpc/v1/ test.testError (POST)`, () => {
it(`should return an error and check error data from JSONRPCClient call`, async () => {
const errorObj = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now an unused variable?

.expect(403)
.expect(errorObj);
const resp = service.testError({ data: "hi" });
return expect(resp).rejects.toThrowError();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to specify that the error should have a certain code/error/message here?

@spion spion merged commit 75acc7e into master Mar 27, 2020
@spion spion deleted the goce/H4C-247_јson-rpc-client branch March 27, 2020 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants