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

gRPC service returns appropriate error message upon input invalidation #118

Open
hienqnguyen opened this issue May 19, 2020 · 0 comments
Open
Labels
enhancement New feature or request M-grpc This issue is related to the gRPC module P2 Priority 2
Milestone

Comments

@hienqnguyen
Copy link
Contributor

If a grpc service has a validateJS configured and an invalid message comes it, it would be nice if CC returns a grpc.status.INVALID_ARGUMENT with the validation error as message.

This is what we're doing currently to achieve the same behavior:

export function wrap<TM, TC, TR>(fieldNames: string[], f: handlerFunc<TM, TC, TR>): handlerFunc<TM, TC, TR> {
    return async (msg: TM, ctx: IDispatchContext<TC>): Promise<TR | any> => {

        const nullRequiredFields = fieldNames.filter((field) => !getField(field, msg));
        if (nullRequiredFields.length > 0) {
            return new Promise((resolve) => resolve({
                code: grpc.status.INVALID_ARGUMENT,
                message: `Required fields are null: [${nullRequiredFields.join(", ")}]`,
            }));
        }

        return await f(msg, ctx);
    };
}

/**
 * This function is to support the supply of dot-notation fields for validation
 * @param fieldAddress The dot delimited field address e.g. "source.location" || "a.b.c.d"
 * @param objectToTraverse The object on which to traverse for a reference
 */
function getField(fieldAddress: string, objectToTraverse: any) {
    return fieldAddress.split(".").reduce(index, objectToTraverse);
}

function index(obj: any, nextAddress: string) { return obj[nextAddress]; }
@sklose sklose added the enhancement New feature or request label May 19, 2020
@sklose sklose added this to the 1.3 milestone May 19, 2020
@plameniv plameniv added the M-grpc This issue is related to the gRPC module label Jun 30, 2020
@sklose sklose modified the milestones: 1.3, 1.4 Oct 13, 2020
@plameniv plameniv added the P2 Priority 2 label Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request M-grpc This issue is related to the gRPC module P2 Priority 2
Development

No branches or pull requests

3 participants