Handle GWS REST server errors

When a call to a Genero Web Services REST function returns a status, you can check for server errors.

The error codes described in the "responses" section of the OpenAPI specification file are generated as constants on the client stub. You can reference these to trap errors in your code.

Error codes generated for expected errors in client stub by fglrestful

If it is an expected error (other than success), a value above 1000 is returned. In this example constants are generated in the client stub from the OpenAPI specification.

# Error codes
PUBLIC CONSTANT C_SUCCESS = 0
PUBLIC CONSTANT C_INVALID = 1001
PUBLIC CONSTANT C_NOTAVAILABLE = 1002
PUBLIC CONSTANT C_MYERROR = 1003
PUBLIC CONSTANT C_INTERNAL_SERVER_ERROR = 1004
If a record type for error handling is generated in the client stub, it can be referenced in your client application.
# generated myErrorErrorType
PUBLIC TYPE myErrorErrorType RECORD
  code INTEGER,
  reason STRING
END RECORD

# My error
PUBLIC DEFINE myError myErrorErrorType

Trapping server errors on the client side

A variable (wsstatus in the example) is defined to trap the status code in the return.

Details of expected errors in the response from the server can be displayed in the reference to the variable defined in the client stub, clientStub.myError in the example.

It is recommended to always handle unexpected errors (CASE OTHERWISE in the code example), as these may arise due to different transport layers. For an unexpected HTTP error, the HTTP status code (200-599) is returned, otherwise -1 is returned.
IMPORT FGL clientStub

# ... function performs call to the client stub function 
FUNCTION myWSRESTcall()
    DEFINE rets INTEGER
    DEFINE wsstatus INTEGER

    DEFINE p_body clientStub.updateUsersRequestBodyType
    LET p_body.users_id = "4" 
    LET p_body.users_name = "new name to be decided"
    # ...

     CALL clientStub.updateUsers(p_body.*) RETURNING wsstatus, rets
     CASE wsstatus
       WHEN clientStub.C_SUCCESS
         DISPLAY "Updated the user:"
       WHEN clientStub.C_INVALID
         DISPLAY "error code   :", clientStub.myError.code
         DISPLAY "error reason :", clientStub.myError.reason
       WHEN  clientStub.C_NOTAVAILABLE
         DISPLAY "error code   :", clientStub.myError.code
         DISPLAY "error reason :", clientStub.myError.reason
       OTHERWISE
         DISPLAY "wsstatus is : ", wsstatus
         DISPLAY "Unexpected error", rets
     END CASE
END FUNCTION