Internal Server Errors are just that; errors that occur in the runtime environment that are not accounted for. For example, the runtime knows how to express a NullPointerException, but it doesn't know how to handle something like a missing translation. You can observe this in action with the following code (as of the time of this answer, anyways):
System.debug(
Id.valueOf('00Y000000000000').getSObjectType()
.getDescribe().getLabel()
);
Once the system encounters a situation it can't handle, the only thing it can safely do is to abort the current transaction, log the error, and inform the user. The reason why your Apex Code can't handle the exception is because it's occurring in the underlying runtime system.
If you've ever had a Blue Screen of Death on a Windows system, you could consider it to be roughly analogous to what's happening with an Internal Server Error. A BSoD halts the system so that no further damage can occur to the system. Similarly, an Internal Server Error halts the transaction so that the underlying system doesn't crash entirely.
If you want to determine the existence of a label, you're going to have to use an alternative API, such as the Metadata API wrapper. In the future, you should be able to use the Apex Metadata API, but this is not available in the current release (with no known date when CustomLabel will be supported).
There are few mistakes in your logic
You have 3 HTTPPost2 requests in your code that validate your email. 3rd is not needed. Thats the one after the
/* part B - Validate EmailAddress */
comment, the one above it does exactly the same (you defined @url, and @body before making 1st HTTPPost2). Cutting this whole part fixes your code:
/* part B - Validate EmailAddress */
set @body = CONCAT('{"email": ', CONCAT('"', @emailAddress, '",'),'"validators": ', '["SyntaxValidator",', '"MXValidator",', '"ListDetectiveValidator"',']}')
set @apitoken = Lookup('AccessToken','AccessToken','id',1)
set @accessToken = CONCAT('BEARER ', @apitoken)
set @url = "https://www.exacttargetapis.com/address/v1/validateEmail"
set @httppost = HTTPPost2(@url, "application/json", @body, true, @callstatus, @response, 'Authorization', @accessToken)
This request was also causing your error, as token for it was empty/null. This was because you tried to get it from DE, but your UpsertData()
function was unable to store it before you tried to retrieve it with Lookup('AccessToken','AccessToken','id',1)
. This led to it being unathorized. Since it was outside of your try{}
it caused LP to error out.
Also - there's easier way to check for 401 unathorized request with just ampscript. For all your HTTPPost2 functions you can set 4th parameter to false instead of true. This will not produce an error so try{} catch{}
will not be needed. Intead it will return status code for your request. You can use that status code in ampscript IF/ELSE
statement to check if it's authorized or not.
set @httppost = HTTPPost2(@url, "application/json", @body, false, @callstatus, @response, 'Authorization', @accessToken)
if @httppost == 401 then
/* refresh and store the token*/
endif
Best Answer
One is enough, unless you want to act differently depending on which query caused the exception.