Although it’s the common belief that there is no way to debug AMPscript and avoid the pesky Error 500, it’s still possible to some extent*.
To make it work, we simply need to place our AMPscript block between 2 server-side Javascript tags and make a separated try…catch statement.
<script runat="server">
Platform.Load("core","1");
try {
</script>
%%[
LookupRows('UnexistingDataExtension','Id',0)
]%%
<script runat="server">
}
catch (err) {
Write("Error Message: " + Stringify(err.message) + Stringify(err.description));
}
</script>
You can also put a console log on the client side to show the errors:
<script runat="client">
console.log(`%%=v(@errorMessage)=%%`);
</script>
Full code
<script runat="server">
Platform.Load("core","1");
try {
</script>
%%[
LookupRows('UnexistingDataExtension','Id',0)
]%%
<script runat="server">
}
catch (err) {
Variable.SetValue("@errorMessage", Stringify(err.message) + Stringify(err.description));
}
</script>
<script runat="client">
console.log(`%%=v(@errorMessage)=%%`);
</script>
This code allows us to print the following errors in the Console:
"The function expression is invalid. See inner exception for detail.
Script: LookupRows('UnexistingDataExtension','Id',0)
Index: 821
ListID: 0
""ExactTarget.OMM.InvalidScriptException: The function expression is invalid. See inner exception for detail.
Script: LookupRows('UnexistingDataExtension','Id',0)
Index: 821
ListID: 0
Error Code: OMM_FUNC_EXPR_INVALID
- from Jint -->
--- inner exception 1---
ExactTarget.OMM.InvalidScriptException: An error occurred when attempting to resolve a function call. See inner exception for detail.
Function Call: LookupRows('UnexistingDataExtension','Id',0)
Index: 907
Content Type: HTML
Substitution Level: Subscriber
Message Context: LandingPage
Error Code: OMM_SCRIPT_SYNTAX_ERR
- from OMMCommon -->
--- inner exception 2---
ExactTarget.OMM.InvalidFunctionException: The Data Extension name for a LookupRows function call is invalid. A Data Extension of this name does not exist.
Data Extension Name: UnexistingDataExtension
Function Call: LookupRows('UnexistingDataExtension','Id',0)
Parameter Name: DataExtensionName
Parameter Ordinal: 1
Error Code: OMM_FUNC_SYNTAX_ERR
- from OMMCommon
Considerations
Note that this is only working if you don’t set the variable to get the rows from the Data Extension. The following code will still result in the Error 500.
%%[
SET DE = LookupRows('UnexistingDataExtension','Id',0)
]%%
*Unfortunately, most of the time the Error 500 remains unavoidable. The alternative solution is to take a look at (or request from the Support) the Marketing Cloud error logs.
Have I missed anything?
Please poke me with a sharp comment below or use the contact form.
Just use the “RaiseError()” ampscript function.
It’s the equivalent of “console.log()” in javascript.
Hi Nick 🙂 No it’s not. If anything, RaiseError() is an equivalent of console.error(), but even then, it’s apples and oranges. RaiseError() is only useful in emails, for send cancellation and custom error handling.