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
hiteshwar marnihiteshwar marni 

custom rest error

@RestResource(urlMapping='/mhc/*')

global  class CaseManager {

 

    @HttpGet

    global static void getCaseById() {

      

        // grab the caseId from the end of the URL
        String jsonResponse = '';
       RestRequest request = RestContext.request;
        RestResponse res = RestContext.response;
        string caseId= request.params.get('caseId');
          
            
        try{
        if(caseId!=null  ){
            
       Case result =  [SELECT CaseNumber,Subject,Status,Origin,Priority

                        FROM Case

                        WHERE Id = :caseId];
                     
             res.responseBody = Blob.valueOf(JSON.serialize(result));
            res.statusCode = 200;
            }
             else{
                
            throw new caserestexception(400,'aaaa');
            }
            }
           
            
        catch(caserestexception ex)
        {
             caserestexception aa;
             res.statusCode = 400;
            jsonResponse = '{"response": {"status": "Failure", "message": "' + ex + '"}}';
            res.responseBody = blob.valueOf(jsonResponse);
            return;
        }
                 
            
            
             
            
               


    }
    
}

I want to get my own error but i'm getting salesforce standard error.how to override standard error

 
Best Answer chosen by hiteshwar marni
Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi hiteshwar marni,

As per my knowledge, you cannot customize exceptions. check below link to understand more.

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

So modify your caserestexception  as below :
public class caserestexception extends Exception{}

And use below code in your service.
@RestResource(urlMapping='/mhc/*')

global  class CaseManager{
    @HttpGet
    global static void getCaseById(){
		// grab the caseId from the end of the URL
        String jsonResponse = '';
		RestRequest request = RestContext.request;
        RestResponse res = RestContext.response;
        string caseId= request.params.get('caseId');
          
            
        try{
			if(caseId!=null  ){            
			Case result =  [SELECT CaseNumber,Subject,Status,Origin,Priority
								FROM Case
								WHERE Id = :caseId];
                     
				res.responseBody = Blob.valueOf(JSON.serialize(result));
				res.statusCode = 200;
            }else{                
				throw new caserestexception('aaaa');
            }
		}catch(caserestexception ex){
             caserestexception aa;
             res.statusCode = 400;
            jsonResponse = '{"response": {"status": "Failure", "message": "' + ex + '"}}';
            res.responseBody = blob.valueOf(jsonResponse);
            return;
        }
    }    
}

Let me know if you need more help.

Regards
Naveen

All Answers

Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi hiteshwar marni,

Please comment your caserestexception class.

Regards
Naveen
hiteshwar marnihiteshwar marni
Hi Naveen Kumar
this is my caserestexception class

public class caserestexception extends Exception{
public string cMessage;
public integer cCode;

    public caserestexception(integer code,string message)
    {
        code=cCode;
        message=cMessage;
    }

}
Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi hiteshwar marni,

As per my knowledge, you cannot customize exceptions. check below link to understand more.

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

So modify your caserestexception  as below :
public class caserestexception extends Exception{}

And use below code in your service.
@RestResource(urlMapping='/mhc/*')

global  class CaseManager{
    @HttpGet
    global static void getCaseById(){
		// grab the caseId from the end of the URL
        String jsonResponse = '';
		RestRequest request = RestContext.request;
        RestResponse res = RestContext.response;
        string caseId= request.params.get('caseId');
          
            
        try{
			if(caseId!=null  ){            
			Case result =  [SELECT CaseNumber,Subject,Status,Origin,Priority
								FROM Case
								WHERE Id = :caseId];
                     
				res.responseBody = Blob.valueOf(JSON.serialize(result));
				res.statusCode = 200;
            }else{                
				throw new caserestexception('aaaa');
            }
		}catch(caserestexception ex){
             caserestexception aa;
             res.statusCode = 400;
            jsonResponse = '{"response": {"status": "Failure", "message": "' + ex + '"}}';
            res.responseBody = blob.valueOf(jsonResponse);
            return;
        }
    }    
}

Let me know if you need more help.

Regards
Naveen
This was selected as the best answer
Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi, are you still facing this issue?