Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/caip-multichain-api' into caip-m…
Browse files Browse the repository at this point in the history
…ultichain-api
  • Loading branch information
jiexi committed Jan 10, 2025
2 parents cf554f8 + fa5b27f commit 298fc54
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 117 deletions.
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

0 comments on commit 298fc54

Please sign in to comment.