OpenWGA 7.10 - OpenWGA Concepts and Features

Design and development » REST web service

Throwing and receiving errors

On TMLScript methods that define REST operations, like those from Query or Custom API, you can throw errors by just throwing a script object featuring the following properties:

  • code: The HTTP error code to report to the client
  • message: A text message defining the error message

An example:

this.get = function(id) {

   

   var data = db().getContentByName(id);

   if (data == null) {

      throw {code:404, message:"No document found with ID: " + id};

   }

   ...

}

The REST client generally receives errors from the service as JSON/XML objects having status "ERROR". A property "error" contains information about the error, including the code and message defined above. It also contains information about the Java error type in properties "type" (The exception type) and "causes" (The exception types and messages up the cause chain).

{
  "status":"ERROR",
  "error":{
    "code":404,
    "type":"javax.ws.rs.WebApplicationException",
    "message":"No document found with ID: 78543",
    "causes":[
      "de.innovationgate.wgpublisher.WGAServerException: Exception running TMLScript expression",
      "de.innovationgate.webgate.api.WGExpressionException: JavaScript exception executing tmlscript: ...",
      "de.innovationgate.wgpublisher.expressions.tmlscript.TMLScriptException: [object Object]"
    ]
  }
}