Web API not returning validation error after "AddUser" node in process model was canceled by exception

I cannot figure out how to return the error message from a process model.  Specifically, the "Add User" node is getting canceled by exception, for example, "Username has already been taken."  I want this error to be returned back into the Web Api service created that starts it... I've successfully been able to add a property to the json that the Web Api responds back to my application that calls it, I named the property "errorMessage."  Of course it's always empty because I haven't been able to figure out how to populate it with the error message mentioned.  Attached are screenshots to further clarify the issue I'm having creating this:

 

 

Actual text: 

with(
a!startProcess(
processModel: cons!CONSTANT_SWF_sendEmailToVerifyAccount,
processParameters: a!fromJson(
http!request.body
),
onSuccess: a!httpResponse(
statusCode: 200,
headers: {
a!httpHeader(
name: "Content-Type",
value: "application/json"
)
},
body: a!toJson(
fv!processInfo
)
),

onError: {
a!httpResponse(
statusCode: 500,
headers: {
a!httpHeader(
name: "Content-Type",
value: "application/json"
)
},
body: a!toJson(
{
error: "There was an error starting the process"
}
)
),
a!startProcess(
processModel: cons!SWF_CREATE_USER_ACCOUNT_RESULT,
processParameters:{
location: "Create User Process Failed TEST"
}
)
}
)
)

 

and screenshot from Web Api response:

 

If anyone knows or has any suggestions for me to correct this problem, let me know.


Kind regards,
Eric

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Hi Eric,

    Agreed with the suggestion provided by @Robert.

    If you are aware that there could be an error condition, then it's better to handle that condition within you WEB-API and return the appropriate http response to the requester.

    Because when you invoke a!startProcess() function in Web-API, then WEB-API only checks whether the process was invoked successfully or not, and if so then return the process info, otherwise return the exception message.

    It doesn't validates whether a particular node of the process is failed or not, because startProcess() invokes a process asynchronously, means Appian will continue to execute httpResponse part i.e. either onSuccess (if successfully invoked) or onError (if invocation failed) to return the response, immediately after invoking the process.

    Hence you should try handling these scenarios in WEB-API as mentioned in below sample code snippet.

    with(
      if(
        isusernametaken(
          loggedInUser() /*http!request.queryParameters.userName -- Will be dynamic*/
        ),
        a!httpResponse(
          statusCode: 500,
          headers: {
            a!httpHeader(
              name: "Content-Type",
              value: "application/json"
            )
          },
          body: a!toJson(
            {
              error: "User Name already exist"
            }
          )
        ),
        a!startProcess(
          processModel: cons!YOUR_PROCESS_MODEL_CONSTANT,
          processParameters: a!fromJson(
            http!request.body
          ) /* Pass the body as per your requirement*/,
          onSuccess: a!httpResponse(
            statusCode: 200,
            headers: {
              a!httpHeader(
                name: "Content-Type",
                value: "application/json"
              )
            },
            body: a!toJson(
              fv!processInfo
            )
          ),
          onError: a!httpResponse(
            statusCode: 500,
            headers: {
              a!httpHeader(
                name: "Content-Type",
                value: "application/json"
              )
            },
            body: a!toJson(
              {
                error: "There was an error starting the process"
              }
            )
          )
        )
      )
    )

    In this way you will be able to handle this scenario and can return the valid response code to the requester, so that they can understand, what exactly happened to their request.

    NOTE: It's just a code sample.

    Hope this will help you.

Reply
  • 0
    Certified Lead Developer

    Hi Eric,

    Agreed with the suggestion provided by @Robert.

    If you are aware that there could be an error condition, then it's better to handle that condition within you WEB-API and return the appropriate http response to the requester.

    Because when you invoke a!startProcess() function in Web-API, then WEB-API only checks whether the process was invoked successfully or not, and if so then return the process info, otherwise return the exception message.

    It doesn't validates whether a particular node of the process is failed or not, because startProcess() invokes a process asynchronously, means Appian will continue to execute httpResponse part i.e. either onSuccess (if successfully invoked) or onError (if invocation failed) to return the response, immediately after invoking the process.

    Hence you should try handling these scenarios in WEB-API as mentioned in below sample code snippet.

    with(
      if(
        isusernametaken(
          loggedInUser() /*http!request.queryParameters.userName -- Will be dynamic*/
        ),
        a!httpResponse(
          statusCode: 500,
          headers: {
            a!httpHeader(
              name: "Content-Type",
              value: "application/json"
            )
          },
          body: a!toJson(
            {
              error: "User Name already exist"
            }
          )
        ),
        a!startProcess(
          processModel: cons!YOUR_PROCESS_MODEL_CONSTANT,
          processParameters: a!fromJson(
            http!request.body
          ) /* Pass the body as per your requirement*/,
          onSuccess: a!httpResponse(
            statusCode: 200,
            headers: {
              a!httpHeader(
                name: "Content-Type",
                value: "application/json"
              )
            },
            body: a!toJson(
              fv!processInfo
            )
          ),
          onError: a!httpResponse(
            statusCode: 500,
            headers: {
              a!httpHeader(
                name: "Content-Type",
                value: "application/json"
              )
            },
            body: a!toJson(
              {
                error: "There was an error starting the process"
              }
            )
          )
        )
      )
    )

    In this way you will be able to handle this scenario and can return the valid response code to the requester, so that they can understand, what exactly happened to their request.

    NOTE: It's just a code sample.

    Hope this will help you.

Children
No Data