Skip to content

Commit

Permalink
chore(rpc): better error reporting when action call args is wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Apr 8, 2024
1 parent da4cf82 commit 9687e52
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 1 addition & 3 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,7 @@ export function isObjectLiteral(obj: any): obj is { [key: string]: any } {
/**
* @public
*/
export function isArray(obj: any): obj is any[] {
return Array.isArray(obj);
}
export const isArray: (obj: any) => obj is any[] = Array.isArray;

/**
* @public
Expand Down
14 changes: 11 additions & 3 deletions packages/rpc/src/server/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ClassType,
collectForMicrotask,
getClassName,
isArray,
isPlainObject,
isPrototypeOfBase,
toFastProperties,
Expand Down Expand Up @@ -461,10 +462,16 @@ export class RpcServerAction {
return response.error(`Validation error for arguments of ${getClassName(classType.controller)}.${body.method}: ${error.message}`);
}

const controllerClassType = this.injector.get(classType.controller, classType.module);
if (!controllerClassType) {
const controllerInstance = this.injector.get(classType.controller, classType.module);
if (!controllerInstance) {
response.error(new Error(`No instance of ${getClassName(classType.controller)} found.`));
}

if (!isArray(value.args)) {
this.logger.error(`Invalid arguments for ${getClassName(classType.controller)}.${body.method} - expected array but got`, value.args);
return response.error(`Invalid arguments for ${getClassName(classType.controller)}.${body.method} - expected array.`);
}

// const converted = types.parametersDeserialize(value.args);
const errors: ValidationErrorItem[] = [];
types.parametersValidate(value.args, { errors });
Expand All @@ -482,7 +489,8 @@ export class RpcServerAction {
response.errorLabel = `Action ${getClassName(classType.controller)}.${body.method} return type serialization error`;

try {
const result = await controllerClassType[body.method](...value.args);
const method = controllerInstance[body.method] as Function;
const result = await method.apply(controllerInstance, value.args);

if (isEntitySubject(result)) {
response.reply(RpcTypes.ResponseEntity, { v: result.value }, types.resultSchema);
Expand Down

0 comments on commit 9687e52

Please sign in to comment.