function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deborah Engelmeyer 4Deborah Engelmeyer 4 

LWC gets blank message from AuraHandledException

I am using api version 49 and throwing an AuraHandledException in Apex code that is called Imperatively from a LWC.

I have verified the exception message is set using Apex unit tests, but I consistently get a blank message in the error handling section of the LWC code.
Apex Code:
Exception ex = new AuraHandledException('');
ex.setMessage('not authorized');
throw ex;

LWC Code:
getData()
      .then((data) => {
        console.log(data);
        // processing for expected result
      })
      .catch((error) => {
        console.error(error);
        if (error.body.message === "not authorized") {
          this.error = noAmgenNewsAuth;
        } else { // error.body.message is always ''
          this.error = cannotConnect;
        }
      });

Am I missing some undocumented trick here?  Or is there a bug?
ANUTEJANUTEJ (Salesforce Developers) 
>> https://salesforce.stackexchange.com/questions/254100/handle-aurahandledexception-in-lightning-web-component

As mentioned in the above link can you try something like below code:

throw new AuraHandledException('not authorized.');

.catch(error => {
        console.error(error);
        let error = TOAST_MESSAGE_ERROR;
        if (error.body.message == "not authorized") {
          error = noAmgenNewsAuth;
        } else { 
          error = cannotConnect;
        }
      }this.showToast(TOAST_TITLE_ERROR, error, TOAST_VARIANT_ERROR);}

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Deborah Engelmeyer 4Deborah Engelmeyer 4
Your suggested code has several errors and I see no material difference between your code and mine.
ANUTEJANUTEJ (Salesforce Developers) 
Oh, Sorry I just edited the code that was in the dev forum thread will it be possible to provide code so as to check further in my developer org and respond.

Looking forward to your response.
Deborah Engelmeyer 4Deborah Engelmeyer 4
I have created a sample project in https://github.com/deb761/lwc-exception-message. I am seeing the same problem with API 50.0. Debbie