🔥 500+ people already subscribed. Why not you? Get our newsletter with handy code snippets, tips, and marketing automation insights.

background shape
background shape

Redirect 500 internal server error inside try catch SSJS block

If you have Redirect function inside the try catch block, either as mixture of SSJS with AMPScript or only in SSJS, it will throw an 500 – Internal Server Error.

Let’s take a look on few examples what can create an error that will take you some quality debugging time to figure it out. And frankly to debug something inside the marketing cloud is one of the most depressing things happened to me in a while.

<script runat=server>
try{
  //do somothing
  throw 'This is error'
}catch(e){
	Redirect('https://example.com',true)
}

</script>
<script runat=server>
 try{
 </script>
  %%[
   /* Do some AMPScript */
   /* e.g. */
 	UpdateSingleSalesforceObject('Contact', 'contactId', 'someField', 'True')
    Redirect(CloudPagesURL(1))
 ]%% 
 <script runat=server>
 }catch(e){
  //do something
 }
</script>

Examples above will throw beautiful 500 – Internal Server Error and leave you guessing what is wrong this time.

To make this work you have to move any redirect functions outside the try-catch block.

<script runat=server>
 var error = false
try{
  //do somothing
  throw 'This is error'
}catch(e){
  	/* you can log e.message into  Data Extension */
	error = true
}
Redirect('https://example.com', true)
</script>

Oh hi there 👋
I have a SSJS skill for you.

Sign up now to get an SSJS skill that can be used with your AI companion

We don’t spam! Read our privacy policy for more info.

Share With Others

The Author
Marcel Szimonisz

Marcel Szimonisz

MarTech consultant

I specialize in solving problems, automating processes, and driving innovation through major marketing automation platforms—particularly Salesforce Marketing Cloud and Adobe Campaign.

Your email address will not be published. Required fields are marked *

Similar posts