Throwing error from cloud code and catching the same in the view where it is being called in a fuction

Hi,

I have a cc that make an api request to third party server and return some data.I want to throw an exception when the response of api is null.How can i do that.
Below is the code which i am using but it is not working.

let deviceDetailsUrl = ${sharedConfig.DATASERVICE[env].url}?sn=${params.serialNumber}&bm=${params.baseModel};

    let deviceDetails = await getData(env, deviceDetailsUrl);

    if (!deviceDetails.results || deviceDetails.results.length < 1) {

        error = "Invalid serial number";

        console.log(error);

        return error;

    }

function validateSerialNumberAndBaseModel() {

 var params = {

    serialNumber:'12313',

    baseModel:'132321312

};

var response = CloudCode.callTask('validate_serialnumber_basemodel', params);

console.log(response);

if(response === "error") {

    notification.error("Invalid serial number");

  //  return;

}

}

// below function is called on button click.
function saveComponents() {

console.log('save component is called');

validateSerialNumberAndBaseModel();

}

Hi @taranbirbajwa

Are you receiving anything in the response var on the app code side?

If not, can you double check that you are returning from the main CC run function with the error value?
Like in the below case

export async function run(params) {
    let deviceDetailsUrl = ${sharedConfig.DATASERVICE[env].url}?sn=${params.serialNumber}&bm=${params.baseModel};

    let deviceDetails = await getData(env, deviceDetailsUrl);

    if (!deviceDetails.results || deviceDetails.results.length < 1) {

        error = "Invalid serial number";

        console.log(error);

        return error;

    }
}

Or maybe any errors in the CC task logs?

15:46:57.212 [TASK:INFO] Invalid serial number

15:46:57.212 [TASK:INFO] Err: Invalid serial number

15:46:57.212 [TASK:ERROR] Invalid serial number

15:46:57.212 [TASK:INFO] Request complete. Response code: 500. Memory used: 54MB.

15:46:57.314 [WEB:INFO] Moved from state ‘CREATED’ to ‘COMPLETED’