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

improve testing of nested errors in multichainMethodCallValidator #5131

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,21 @@ describe('multichainMethodCallValidatorMiddleware', () => {
mockNext,
(error) => {
try {
expect(error).toBeDefined();
expect((error as JsonRpcError).message).toBe(
'Invalid method parameter(s).',
);
expect((error as JsonRpcError).code).toBe(-32602);
expect((error as JsonRpcError).data).toStrictEqual([
{
code: -32602,
message: 'scope is required, but is undefined',
data: {
param: 'scope',
path: [],
schema: {
pattern: '[-a-z0-9]{3,8}(:[-_a-zA-Z0-9]{1,32})?',
type: 'string',
},
got: undefined,
},
const rpcError = error as JsonRpcError & { data: JsonRpcError[] };
expect(rpcError.message).toBe('Invalid method parameter(s).');
expect(rpcError.code).toBe(-32602);
expect(rpcError.data[0].data).toStrictEqual({
got: undefined,
param: 'scope',
path: [],
schema: {
pattern: '[-a-z0-9]{3,8}(:[-_a-zA-Z0-9]{1,32})?',
type: 'string',
},
]);
});
expect(rpcError.data[0].message).toBe(
'scope is required, but is undefined',
);
resolve();
} catch (e) {
reject(e);
Expand Down Expand Up @@ -125,31 +120,26 @@ describe('multichainMethodCallValidatorMiddleware', () => {
mockNext,
(error) => {
try {
expect(error).toBeDefined();
expect((error as JsonRpcError).message).toBe(
'Invalid method parameter(s).',
);
expect((error as JsonRpcError).code).toBe(-32602);
expect((error as JsonRpcError).data).toStrictEqual([
{
code: -32602,
data: {
got: undefined,
param: 'request',
path: [],
schema: {
properties: {
method: {
type: 'string',
},
params: true,
},
type: 'object',
const rpcError = error as JsonRpcError & { data: JsonRpcError[] };
expect(rpcError.message).toBe('Invalid method parameter(s).');
expect(rpcError.code).toBe(-32602);
expect(rpcError.data[0].data).toStrictEqual({
got: undefined,
param: 'request',
path: [],
schema: {
properties: {
method: {
type: 'string',
},
params: true,
},
message: 'request is required, but is undefined',
type: 'object',
},
]);
});
expect(rpcError.data[0].message).toBe(
'request is required, but is undefined',
);
resolve();
} catch (e) {
reject(e);
Expand Down Expand Up @@ -191,30 +181,25 @@ describe('multichainMethodCallValidatorMiddleware', () => {
mockNext,
(error) => {
try {
expect(error).toBeDefined();
expect((error as JsonRpcError).message).toBe(
'Invalid method parameter(s).',
);
expect((error as JsonRpcError).code).toBe(-32602);
expect((error as JsonRpcError).data).toStrictEqual([
{
code: -32602,
data: {
got: {
method: {},
params: {
test: 'test',
},
},
param: 'request',
path: ['method'],
schema: {
type: 'string',
},
const rpcError = error as JsonRpcError & { data: JsonRpcError[] };
expect(rpcError.message).toBe('Invalid method parameter(s).');
expect(rpcError.code).toBe(-32602);
expect(rpcError.data[0].data).toStrictEqual({
got: {
method: {},
params: {
test: 'test',
},
message: 'request.method is not of a type(s) string',
},
]);
param: 'request',
path: ['method'],
schema: {
type: 'string',
},
});
expect(rpcError.data[0].message).toBe(
'request.method is not of a type(s) string',
);
resolve();
} catch (e) {
reject(e);
Expand Down Expand Up @@ -296,31 +281,26 @@ describe('multichainMethodCallValidatorMiddleware', () => {
mockNext,
(error) => {
try {
expect(error).toBeDefined();
expect((error as JsonRpcError).code).toBe(-32602);
expect((error as JsonRpcError).message).toBe(
'Invalid method parameter(s).',
);
expect((error as JsonRpcError).data).toStrictEqual([
{
code: -32602,
data: {
got: undefined,
param: 'notification',
path: [],
schema: {
properties: {
method: {
type: 'string',
},
params: true,
},
type: 'object',
const rpcError = error as JsonRpcError & { data: JsonRpcError[] };
expect(rpcError.message).toBe('Invalid method parameter(s).');
expect(rpcError.code).toBe(-32602);
expect(rpcError.data[0].data).toStrictEqual({
got: undefined,
param: 'notification',
path: [],
schema: {
properties: {
method: {
type: 'string',
},
params: true,
},
message: 'notification is required, but is undefined',
type: 'object',
},
]);
});
expect(rpcError.data[0].message).toBe(
'notification is required, but is undefined',
);
resolve();
} catch (e) {
reject(e);
Expand Down Expand Up @@ -465,21 +445,15 @@ describe('multichainMethodCallValidatorMiddleware', () => {
mockNext,
(error) => {
try {
expect(error).toBeDefined();
console.log('error in test', error);
expect((error as JsonRpcError).message).toBe(
'Invalid method parameter(s).',
const rpcError = error as JsonRpcError & { data: JsonRpcError[] };
expect(rpcError.message).toBe('Invalid method parameter(s).');
expect(rpcError.code).toBe(-32602);
expect(rpcError.data[0].data).toStrictEqual({
method: 'unknown_method',
});
expect(rpcError.data[0].message).toBe(
'The method does not exist / is not available.',
);
expect((error as JsonRpcError).code).toBe(-32602);
expect((error as JsonRpcError).data).toStrictEqual([
{
code: -32601,
message: 'The method does not exist / is not available.',
data: {
method: 'unknown_method',
},
},
]);
resolve();
} catch (e) {
reject(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ const transformError = (
error.path.length > 0 ? `.${error.path.join('.')}` : ''
} ${error.message}`;

return {
code: -32602, // TODO: could be a different error code or not wrapped in json-rpc error, since this will also be wrapped in a -32602 invalid params error
return rpcErrors.invalidParams({
message,
data: {
param: param.name,
path: error.path,
schema: error.schema,
got,
},
};
});
};

const v = new Validator();
Expand Down Expand Up @@ -66,26 +65,13 @@ const multichainMethodCallValidator = async (
!isObject(methodToCheck) ||
!('params' in methodToCheck)
) {
return [
{
code: -32601,
message: 'The method does not exist / is not available.',
data: {
method,
},
},
];
return [rpcErrors.methodNotFound({ data: { method } })] as JsonRpcError[];
}

const errors: JsonRpcError[] = [];
for (const param of methodToCheck.params) {
if (!isObject(params)) {
return [
{
code: -32602,
message: 'Invalid method parameter(s).',
},
];
return [rpcErrors.invalidParams()] as JsonRpcError[];
}
const p = param as ContentDescriptorObject;
const paramToCheck = params[p.name];
Expand Down
Loading