Sobes.tech
Junior — Senior

Business logic correction in request handler

livecode

Task condition

In the project, there is a function that implements some business logic when processing API requests. It always receives correct data as input, but when working with the SERVICE::NUMBER1 service, an error constantly occurs. It is necessary to analyze the code, identify, and describe the places where the logic is implemented incorrectly.

json::object Session::ProcessRequest(const json::object& body, const SERVICE& service, const API& api)
{
    json::object requestJson = body;
    requestJson["version"] = api;

    switch (service)
    {
        case SERVICE::NUMBER2:
        {
            requestJson["search_type"] = "1";
            requestJson = Authorization(requestJson);
            break;
        }
        case SERVICE::NUMBER1:
        {
            requestJson["search_type"] = "2";

            switch (api)
            {
                case API::V1:
                    break;
                case API::V2:
                    requestJson = Authorization(requestJson);
                    break;
                default:
                    return HandleErrorMessage(fmt::v8::format("Invalid API version for Audio: {}", api));
            }
            break;
        }
        default:
            return HandleErrorMessage(fmt::v8::format("Invalid service type: {}", service));
    }

    if (requestJson.contains("status") && requestJson.at("status").is_string() && requestJson.at("status").as_string() == "error")
        return requestJson;

    return SendMessage(requestJson, config.requestName, config.resultName);
}

Task: identify which parts of the code lead to incorrect behavior when processing SERVICE::NUMBER1, and suggest corrections.