4

I am calling an apex controller, that has several try-catch blocks that throw exceptions depending on the error or situation, (i am also throwing outside of try catch blocks) this is an example:

try{
                    tipo = [
                        SELECT  id
                        FROM    Tipo_Comprobante_Movimiento_Fondo__c 
                        WHERE   Tipo__c = 'Cobranzas' AND 
                                Empresa__c = :empresaSelected AND
                                Cuenta_Contable__c = :cc
                    ];
                }catch(Exception e){
                    throw new AuraHandledException(System.Label.consulta_saldo_cliente_18);
                }  

Here, I'm sending a label already saved in custom labels, but there are other thrown with normal strings:

throw new AuraHandledException('No se pudo completar el proceso');

For some reason, the message of the error from the LWC side, is "script thrown exception"...

enter image description here

this happened to me today for the first time.. I was already working with these exceptions and this never happened.

3
  • actually, my mistake. I am throwing exceptions outside of try catch blocks also Oct 19, 2021 at 16:48
  • and, I checked the stacktrace of the error and it points to the catch block, so it is actually throwned inside the try catch Oct 19, 2021 at 16:49
  • please provide code of apex method and js Oct 19, 2021 at 16:50

1 Answer 1

16

I recollect that this is a design quirk of AuraHandledException and that e.g. this is needed (constructor argument probably doesn't matter):

AuraHandledException e = new AuraHandledException(System.Label.consulta_saldo_cliente_18);
e.setMessage(System.Label.consulta_saldo_cliente_18);
throw e;

which is not convenient so best to wrap in a method:

private AuraHandledException newMessageException(String message) {
    AuraHandledException e = new AuraHandledException(message);
    e.setMessage(message);
    return e;
}

so you can then conveniently:

throw newMessageException(System.Label.consulta_saldo_cliente_18);
1
  • 1
    thanks! this works, I had already done this to test aurahandledexceptions but I thought the whole point of this type of exception was to be captured by LWC and getting the messages correctly. I don't know how this happened all of the sudden Oct 20, 2021 at 14:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .