mardi 23 juin 2020

How to avoid multiple if else block so that the code does not smell?

There are a lot of if-else statements in my code. What is the best way to avoid so many if-else statements? Below is the code snippet. Now, I need to check if WWW-Authenticate header returns value signature_invalid then I need to log and return a different error message and if WWW-Authenticate header returns value token_exppured then I need to log a different error message which will again add 2 more ifs. Can anyone Help me how can I avoid this?

if (e.getRawStatusCode() == NOT_FOUND) {
    logger.log(
        log,
        LogLevel.ERROR,
        MISSING_VALID_ID_ERROR_MSG + " : " + e.toString(),
        viewRequest);
    String errorDetails = createNotFoundDetails(appl, transactionId);
    updateIdentifier(rcvLog, true, request, identifier);
    return createErrorViewResponseReply(MISSING_VALID_ID_ERROR_MSG, errorDetails);

} else if (e.getRawStatusCode() == UNAUTHORIZED) {
    logger.log(
        log,
        LogLevel.ERROR,
        UNABLE_TO_REACH_ERROR_MSG + " : " + e.toString(),
        viewRequest);
    if (e.getResponseHeaders() != null && e.getResponseHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE != null)) {
        logger.log(
            log,
            LogLevel.ERROR,
            INVALID_TOKEN_ERROR_MSG + " : " + e.getResponseHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE),
            viewRequest);
    }
    updateIdentifier(rcvLog, false, request, identifier);
    return createErrorViewResponseReply(
        UNABLE_TO_REACH_ERROR_MSG,
        INVALID_TOKEN_ERROR_DETAILS);
}

Aucun commentaire:

Enregistrer un commentaire