You need to sign in to do that
Don't have an account?
hiteshwar 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
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
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 :
And use below code in your service.
Let me know if you need more help.
Regards
Naveen
All Answers
Please comment your caserestexception class.
Regards
Naveen
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;
}
}
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 :
And use below code in your service.
Let me know if you need more help.
Regards
Naveen