Open Source API Gateway

500 Internal Server Error: Error Evaluating setBody Expression

Membrane API Gateway returns 500 Internal Server Error with the title Error evaluating expression! when the setBody plugin cannot evaluate its value expression. The expression is part of the gateway configuration, even when the content of the current message triggered the error.

The Error Response

The response contains the status 500 Internal Server Error, the title Error evaluating expression!, the problem type https://membrane-api.io/problems/internal and a link to this page. In development mode, it also contains the failing expression as expression and the message of the root cause. The type internal means that the gateway could not complete the processing of the exchange. The plugin can fail in the request or the response flow. In both cases the client receives 500. In production mode, the title is replaced by Internal Server Error and the details are left out.

What the SetBody Plugin Does

The setBody plugin replaces the message body with the result of value. By default, value is a template with SpEL expressions in ${…}. With language, the expressions can also be jsonpath, xpath or groovy. The expression is evaluated for every message, so errors only show up at runtime.

Common Causes

The SpEL Expression Refers to Something Missing

The expression refers to a property, method or variable that does not exist, or calls a method on a null value, such as a header the message does not have.

The Body Does Not Fit the Language

With language: jsonpath, the body is not valid JSON. With language: xpath, the body is not well-formed XML.

The Groovy Expression Throws

With language: groovy, the expression throws, for example with a MissingPropertyException or a NullPointerException.

The Expression Is Syntactically Invalid

The expression is not valid for the chosen language, and the error only appears when it is evaluated.

An Unexpected Exception in the Plugin

Any other exception that the setBody plugin does not turn into an error response itself is reported under the same URL by Membrane's generic error handler. The detail then reads Aborting! Exception caused in … flow. and names the plugin, the request URI and the flow. Unless the exception points to your configuration or to the message, this usually indicates a bug in Membrane.

How to Fix It

  1. Check the expression field in the response (development mode) and the log line While evaluating expression … with the root cause.
  2. Correct the expression, or make it null-safe.
  3. If the body format depends on the client, validate the body before setBody, or wrap setBody in an if that checks the content type.

With the Elvis operator, a missing header does not break the SpEL expression:

api:
port: 2000
flow:
- request:
- setBody:
value: |
Hello ${header['X-Name'] ?: 'unknown'}

- return:
status: 200
api:
  port: 2000
  flow:
    - request:
        - setBody:
            value: |
              Hello ${header['X-Name'] ?: 'unknown'}
    - return:
        status: 200

Hide Details in Production

By default, Membrane runs in development mode and adds details to error responses that help you find the problem. These details may reveal information about your setup. In production, turn them off:

configuration:
production: true
configuration:
  production: true

Add the configuration document to your apis.yaml and separate it from the APIs with ---. The title then reads Internal Server Error, and instead of the details the response only contains a log key. Search the gateway log for this key to find the exception.

If the plugin is nested inside a request, response or abort element and throws an unexpected exception, the exception message, the stack trace and the link to this page are currently sent even in production mode. Fix the failing plugin rather than relying on production mode to hide them.

Related Documentation

Configuration reference:

Tutorials with runnable examples:

← All Membrane error messages

Still stuck? See the troubleshooting guide or ask on GitHub Issues. For commercial support, contact info@predic8.de.