Review my error handler and HAPI new year

Hello Guys and HAPI new year ^^

I'm here because I'm fairly new on Apigee and in the API Management world and would love your toughts, insights on the error handler i developed for my company ^^

It's a simple sharedFlow with a JS and an AM. Here's the JS :

 

const customCode = context.getVariable("customCode");
const faultName = context.getVariable("fault.name");
var customResponse = {};

// 400 : customCode | Payload : code, description, reason (depending on errors), errors[]
if (customCode == 400 || context.getVariable("error.status.code") == 400) {

    customResponse["code"] = 400;
    customResponse["description"] = "The request has invalid parameters."
    
    customResponse["reason"] = "BAD_REQUEST"
    // send 3 arrays (reasons, messages, keys)
    if (context.getVariable("customKeyList")) {
        var customKeyList = context.getVariable("customKeyList").split(",");
        var customReasonList = context.getVariable("customReasonList").split(",");
        // Messages should not include a ","
        var customMessageList = context.getVariable("customMessageList").split(",");
        var errors = [];
        for (var i = 0; i < customKeyList.length; i++) {
            var error = {
                reason: customReasonList[i],
                message: customMessageList[i],
                key: customKeyList[i]
            };
            errors.push(error);
        };
        customResponse["errors"] = errors;
    }
}
// 401 : customCode or faultName | Payload : code, description, reason (depending on faultName)
else if (customCode == 401 || context.getVariable("error.status.code") == 401) {

    customResponse["code"] = 401

    if (!context.getVariable("customDescription")) customResponse["description"] = "You are not authorized to access this resource."
    else customResponse["description"] = context.getVariable("customDescription")

    if (!context.getVariable("customReason")) customResponse["reason"] = faultName.split(/\.?(?=[A-Z])/).join('_').toUpperCase()
    else customResponse["reason"] = context.getVariable("customReason")

}
// 403 : customCode or faultName | Payload : code, description, reason
else if (customCode == 403 || context.getVariable("error.status.code") == 403) {

    customResponse["code"] = 403

    if (!context.getVariable("customDescription")) customResponse["description"] = "Access to this resource is forbidden."
    else customResponse["description"] = context.getVariable("customDescription")

    if (!context.getVariable("customReason")) customResponse["reason"] = faultName.split(/\.?(?=[A-Z])/).join('_').toUpperCase()
    else customResponse["reason"] = context.getVariable("customReason")

}
// 404 : customCode | Payload : code, description, reason
else if (customCode == 404 || context.getVariable("error.status.code") == 404) {

    customResponse["code"] = 404

    if (!context.getVariable("customDescription")) customResponse["description"] = "The resource you are trying to access does not exist."
    else customResponse["description"] = context.getVariable("customDescription")

    if (!context.getVariable("customReason")) customResponse["reason"] = "NOT_FOUND"
    else customResponse["reason"] = context.getVariable("customReason")

}
// 405 : customCode | Payload : code, description, reason
else if (customCode == 405 || context.getVariable("error.status.code") == 405) {

    customResponse["code"] = 405

    if (!context.getVariable("customDescription")) customResponse["description"] = "The request method is not supported by the requested resource."
    else customResponse["description"] = context.getVariable("customDescription")

    if (!context.getVariable("customReason")) customResponse["reason"] = "METHOD_NOT_ALLOWED"
    else customResponse["reason"] = context.getVariable("customReason")

}
// 409 : customCode | Payload : code, description, reason
else if (customCode == 409 || context.getVariable("error.status.code") == 409) {

    customResponse["code"] = 409

    if (!context.getVariable("customDescription")) customResponse["description"] = "The resource already exist."
    else customResponse["description"] = context.getVariable("customDescription")

    if (!context.getVariable("customReason")) customResponse["reason"] = "CONFLICT"
    else customResponse["reason"] = context.getVariable("customReason")

}
// 429 : customCode or faultName | Payload : code, description, reason
else if (customCode == 429 || context.getVariable("error.status.code") == 429) {

    customResponse["code"] = 429

    if (!context.getVariable("customDescription")) customResponse["description"] = "The user has sent too many requests."
    else customResponse["description"] = context.getVariable("customDescription")

    if (!context.getVariable("customReason")) customResponse["reason"] = faultName.split(/\.?(?=[A-Z])/).join('_').toUpperCase()
    else customResponse["reason"] = context.getVariable("customReason")

}
// 500 : customCode | Payload : code, description, reason
else if (customCode == 500 || context.getVariable("error.status.code") == 500) {

    customResponse["code"] = 500

    if (!context.getVariable("customDescription")) customResponse["description"] = "An unknown error occured."
    else customResponse["description"] = context.getVariable("customDescription")

    if (!context.getVariable("customReason")) customResponse["reason"] = "INTERNAL_SERVER_ERROR"
    else customResponse["reason"] = context.getVariable("customReason")

}
// Everything else 500
else {
    customResponse["code"] = 500

    if (!context.getVariable("customDescription")) customResponse["description"] = "An unknown error occured."
    else customResponse["description"] = context.getVariable("customDescription")

    if (!context.getVariable("customReason")) customResponse["reason"] = "INTERNAL_SERVER_ERROR"
    else customResponse["reason"] = context.getVariable("customReason")
}

customResponse["x-request-id"] = context.getVariable("messageid")
context.setVariable("customCode", customResponse["code"]);
context.setVariable("response.status.code", customResponse["code"]);
customResponse = JSON.stringify(customResponse);
context.setVariable("customHeader", "application/json");
context.setVariable("customResponse", customResponse);

 

The AM only sets the response to customResponse.

I tried making it the more serialized possible. Backend responses populates the appropriate custom variables at the proxy-level.

To my knowledge :

- Only the 400 code needs or should be detailed.

- In general, there isn't a noticeable performance gain to chosing AM over JS (Would more details on perfs)

Thank you in advance.

@dchiesa1 

0 0 59
0 REPLIES 0