Open Source API Gateway

500 Internal Server Error: For Expression Failed

Membrane API Gateway returns 500 Internal Server Error when the for plugin cannot evaluate its in expression against the current request or response. The fault is usually in the gateway configuration, sometimes in a message body that is not valid JSON or XML.

The Error Response

The response contains the status 500 Internal Server Error, a detail that describes the evaluation problem, the problem type https://membrane-api.io/problems/internal and a link to this page. The type internal means that the gateway could not complete the processing of the message. The error can occur in the request and in the response flow; in both cases the client receives a 500. In production mode, the title is replaced by Internal Server Error and the details are left out.

What the For Plugin Does

The for plugin evaluates the expression in in to a list and runs its nested flow once for every element, which is available as it. The expression language is set with language: spel, jsonpath, xpath or groovy.

api:
port: 2000
flow:
- for:
in: $.fruits
language: jsonpath
flow:
- setBody:
value: ${toJSON(it)}
- call:
method: POST
url: https://api.predic8.de/shop/v2/products
api:
  port: 2000
  flow:
    - for:
        in: $.fruits
        language: jsonpath
        flow:
          - setBody:
              value: ${toJSON(it)}
          - call:
              method: POST
              url: https://api.predic8.de/shop/v2/products

See scripting with Groovy, JavaScript and SpEL for the expression languages.

Common Causes

The SpEL Expression Fails

The expression refers to a method that does not exist, fails at runtime (for example a property access on a null value), or yields a value that cannot be converted. The detail names the problem, e.g. Method not found in expression ... or Type converter not found .... If a Membrane built-in function fails, it is named in the function field of the response.

The JSONPath Is Invalid or the Body Is Not JSON

With language: jsonpath, the path is syntactically invalid (Invalid JSONPath.), or the body cannot be read as the expected JSON (Error evaluating JSONPath).

The XPath Is Invalid or the Body Is Not XML

With language: xpath, the expression is invalid, uses a namespace prefix that is not declared, or the body has characters before the XML declaration or after the root element.

An Unexpected Exception in the Plugin

Any other exception thrown by the plugin, including a runtime error in a groovy expression, 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. Operator: check the in expression and its language against the message the plugin receives in the flow where it is placed.
  2. Operator: for XPath with prefixes, declare the namespaces in the xmlConfig of the enclosing API.
  3. Client: when the detail shows a body parsing problem, send a well-formed JSON or XML body.
  4. Enable development mode to see the expression and the error in the response. In production, look up the log key of the response in the gateway log.

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 response then 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, 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.