How to fix the "throw" of an exception locally detected?

In this function that handles the REST API call, any of the called functions to process the parts of the request may throw an error to signal that the error code should be sent as a response. However, the function itself can also detect an error, after which it must go to the exception handling block.

static async handleRequest(req) {
    try {
        let isAllowed = await checkIfIsAllowed(req);
        if (!isAllowed) {
            throw new ForbiddenException("You're not allowed to do that.");
        }
        let result = await doSomething(req); // can also raise exceptions
        sendResult(result);
    } catch(err) {
        sendErrorCode(err);
    }
}

Webstorm will underline with the throwfollowing message:'throw' of exception caught locally. This inspection reports any instances of JavaScript throw statements whose exceptions are always caught by containing try statements. Using throw statements as a "goto" to change the local flow of control is likely to be confusing.

However, I'm not sure how to reorganize the code to improve the situation.

I could copy the code from the block catchto the check if, but I believe that this will make my code less readable and harder to maintain.

, isAllowed , , , , , Webstorm.

Webstorm ?

+14
5

- , isAllowed , , - sendErrorCode. , , - .

, , - throw/catch:

static async handleRequest(req) {
    try {
        let isAllowed = await checkIfIsAllowed(req);
        if (!isAllowed) {
            sendErrorCode("You're not allowed to do that.");
            return;
        }
        let result = await doSomething(req); // can also raise exceptions
        sendResult(result);
    } catch(err) {
        sendErrorCode(err);
    }
}

catch if, , .

, , , .

+19

. DRY, , sendError, . , , . , .

async function doSomethingOnAllowedRequest(req) {
    let isAllowed = await checkIfIsAllowed(req);
    if (!isAllowed) {
       throw new ForbiddenException("You're not allowed to do that.");
    }
    doSomething(req);
}
static async handleRequest(req) {
    try {
        let result = await doSomethingOnAllowedRequest(req);
        sendResult(result);
    } catch(err) {
        sendErrorCode(err);
    }
}
0

" ?"

, , , , , , , , .

@James , @matchish , . , . "", " ", , : " , , ". .

, , , DRY, "", , , . sendErrorCode . , , , sendErrorCode.

@James Thorpe, , , , sendErrorCode , :

static async handleRequest(req) {
    try {
        let isAllowed = await checkIfIsAllowed(req);
        if (!isAllowed) {
            sendErrorCode(new ForbiddenException("You're not allowed to do that."));
            return;
        }
        let result = await doSomething(req); // can also raise exceptions
        sendResult(result);
    } catch(err) {
        sendErrorCode(err);
    }
}

- !isAllowed . . , !isAllowed , , , , , , isAllowed.

@matchish doSomethingOnAllowedRequest , , , , . DRY, , . , , , .

0

, IDE, .

- . ( ) nodejs, . , IDE , .

- . - . .

0

, , ( , ). Catch

"The reason your catch try block does not work is because the ajax request is asynchronous. The catch try block will be executed before the Ajax call and send the request itself, but the error occurs when the result is returned, AT LATER POINT IN TIME.

When the catch try block is executed, no error occurs. When an error occurs, there is no attempt to catch. If you need to try catch for ajax requests, always put ajax try catch blocks inside the success callback, NEVER outside it. "

-3
source

Source: https://habr.com/ru/post/1688402/


All Articles