How to change an Error Message from a Backend

This recipe demonstrates how to dynamically change a server's error response to a different format.
We will use Problem JSON as return format for this recipe.

Ingredients

Configuration

For more information on inline expressions and scripting in Membrane, see: Scripting.

<api port="2000">
  <response>             <!-- Regex for 4XX -->
    <if test="statusCode matches '4.*'" language="spel">
      <template contentType="application/json">
        {
          "type": "https://membrane-api.io/error/",
          "title": "${exc.response.statusMessage}",
          "status": ${exc.response.statusCode}
        }
      </template>
    </if>
  </response>
  <target url="localhost:2001" />
</api>

<api port="2001" name="mock server">
  <return statusCode="405" />
</api>

Resources

If Documentation Examples
Template Documentation Examples