본문 바로가기

Develop/Salesforce

[Salesforce] how to set exception message

반응형

This is useful when you want to display the message of Exception handling in the controller on the screen. 

Exceptions can be top-level classes, so your custom exception class must extend exception.

All exception classes extend the system-defined base class Exception, and therefore, inherits all common Exception methods.

 

Let me give you an example of how this is the way I used it.

 

1) At the top of the class, the custom exception class inherits the Exception class.

global class ExmapleClass{
	public class customException extends Exception {} 
    
    //your code..
}

2) Set the condition of the Exception and send the Exception message.

@AuraEnabled
public static Map<String, Object> myMethod() {
	
    try{
    	List<Contact> cons = [SELECT id, ExampleNum__c, ExampleText__c from Contact];
        for(Contact con : cons){
            if(con.ExampleNum__c > 10){
                throw new customException('First Exception');
            }

            if('Test' != con.ExampleText__c){
                throw new customException('Second Exception');
            }
        }
        
        return new Map<String, Object>{
            'status' => 'SUCCESS'
            , 'params' => recordId
        };
        
    }catch(Exception e){
       return new Map<String, Object>{
            'status' => 'ERROR'
            , 'exception' => new Map<String, Object>{
                'message' => e.getMessage()
                , 'line' => e.getLineNumber()
                , 'type' => e.getTypeName()
            }
        }; 	
    }
}

3) Use Toast to display your error message.

 

Controller.js

({
    "echo" : function(cmp) {
        var action = cmp.get("c.serverEcho");
        
        action.setParams({ firstName : cmp.get("v.firstName") });

        action.setCallback(this, function(response) {
        	const RETURN_VALUE =  response.getReturnValue();
            var state = response.getState();
            if (state === "SUCCESS") {
            
                alert("From server: " + RETURN_VALUE);
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    component.find('notifLib').showToast({
                        'message': RETURN_VALUE.exception.message
                        , 'variant':'error'
                    });
                } else {
                    console.log("Unknown error");
                }
            }
        });
        
        $A.enqueueAction(action);
    }
})

.cmp

 <lightning:notificationsLibrary aura:id="notifLib"/>

 

4) Result

 

 

>참고

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_custom.htm

 

Create Custom Exceptions | Apex Developer Guide | Salesforce Developers

You can’t throw built-in Apex exceptions. You can only catch them. But with custom exceptions, you can throw and catch them in your methods. Custom exceptions enable you to specify detailed error messages and have more custom error handling in your catch

developer.salesforce.com

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm

 

Calling a Server-Side Action | Lightning Aura Components Developer Guide | Salesforce Developers

Call a server-side controller action from a client-side controller. In the client-side controller, you set a callback, which is called after the server-side action is completed. A server-side action can return any object containing serializable JSON data.

developer.salesforce.com

 

반응형