Example: error handling with WSThrows

Error handling is supported with the WSThrows and WSError attributes.

In this function the @userError in WSThrows references the "userError" modular variable.

You code to trap the error by updating the "message" field with a description of the error, and calling the SetRestError() method to return it.

Example using WSError with WSThrows

IMPORT com

PUBLIC DEFINE userError RECORD ATTRIBUTE(WSError="User error")
  message STRING
END RECORD

PUBLIC FUNCTION GetUserNameById( id STRING ATTRIBUTE(WSQuery) )
  ATTRIBUTES(WSGet,
             WSThrows="400:@userError")
  RETURNS STRING
    DEFINE s STRING
    WHENEVER ERROR CONTINUE
      SELECT lname INTO s FROM customers WHERE @id  = id
    WHENEVER ERROR STOP
    IF sqlca.sqlcode == NOTFOUND THEN
        LET userError.message = SFMT("Could not find user id :%1",id)
        CALL com.WebServiceEngine.SetRestError(400,userError)
    END IF
    RETURN s
END FUNCTION