moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
291 stars 119 forks source link

Axios timeout handling not respected instead molecular middleware response is sent #284

Closed rohitpatidar007 closed 2 years ago

rohitpatidar007 commented 2 years ago

Getting similar issue on molecular web ^0.9.1 as https://github.com/moleculerjs/moleculer-web/issues/62 . My scenario :

  1. Calling POST api using axios and sending response to my client with some customization, Sample code given below :
    
    async function sentRequest(axiosConfig) {
    let result = {},
        finalResult = {
            httpStatus: 500,
            status : 'FAILURE',
            //error : {},
            data : {}
        };
    try {
        result = await axiosReq(axiosConfig);
        finalResult.httpStatus = result.status,
        finalResult.status = 'SUCCESS',
        finalResult.data = result.data;
    } catch (error) {
        finalResult.error = { errorCode: '', errorMessage: '' };
        if (error.response && error.response.status) {
            finalResult.error.errorCode = error.response.status,
            finalResult.error.errorMessage = error.error_description ? error.error_description : error.message,
            finalResult.httpStatus = error.response.status;
            finalResult.data = error.response.data;
        } else {
            finalResult.error.errorCode = error.errno,
            finalResult.error.errorMessage = "Unknown error occured" + ( error.code ? (" - " +  error.code) : "");
        }
    }
    return finalResult;
    }
2.  When a timeout occurs response gets sent as is which is something like below (my custom code written in catch block is triggered after some time after response is already sent to client ) :

{ "name": "RequestTimeoutError", "message": "Request is timed out when call 'auth.initiateOTPFlow' action on '60015702438-17396' node.", "code": 504, "type": "REQUEST_TIMEOUT", "data": { "action": "auth.initiateOTPFlow", "nodeID": "60015702438-17396" } }


This is causing uneven response to the my client.

Below is the error :
[2021-11-29T12:10:52.882Z] ERROR 60015702438-17396/API: Request error! RequestTimeoutError : Request is timed out when call 'auth.initiateOTPFlow' action on '60015702438-17396' node.
RequestTimeoutError: Request is timed out when call 'auth.initiateOTPFlow' action on '60015702438-17396' node. at D:\developement\ReactJs-Projects\selfservice-api\node_modules\moleculer\src\middlewares\timeout.js:42:14
at async Service.callAction (D:\developement\ReactJs-Projects\selfservice-api\node_modules\moleculer-web\src\index.js:616:16)
at async D:\developement\ReactJs-Projects\selfservice-api\node_modules\moleculer-web\src\index.js:443:22
Data: { action: 'auth.initiateOTPFlow', nodeID: '60015702438-17396' }
icebob commented 2 years ago

Please create a repro code.