0

This is sort of an extension of this question here. I have a policy that calls a REST API. The API returns an error message and this message needs to be localized.

One way is to of course get the API to return a localized message, but is there a way for the CustomPolicy itself to localize the error code? According to the CustomPolicy Docs, a REST API can send an error code along with the Conflict error code. Our thinking was to use this error code as a key and select a localized message (from the messageValue enum mentioned in the answer in the link).

However, we can't seem to capture/handle the error data returned by the API. The Policy seems to handle error codes by itself and we would like to know if it is possible to inject localized exception/error messages from the policy itself.

Thanks in advance!

Edit: A little more information about the setup. We have a TechnicalProfile that has a DisplayWidget and a ValidationTechnicalProfile. The DisplayWidget is used for entering & verifying the user's phone/email and the ValidationTechnicalProfile makes the final call to the RestAPI with all the user's information to register him/her. This RestAPI call output is what we want to localize.

The suggestion in the linked SO question, from what I understand, is that we integrate another DisplayClaim (that references an enum) in the DisplayWidget, and depending on the ErrorCode returned by the call, change it to display the appropriate code. However, as per my understanding, this would also require editing the API to return only 200 along with a code. This code would indicate the true nature of the result - success or a code for one of the enums to be displayed.

Our aim therefore is to check if there is a way to follow the Policy's flow (disrupt the SignUp/SignIn process) but at the same time localize the API's displayed response.

vronzeur
  • 33
  • 6
  • Hi @vronzeur. What happens if you add an output claims transformation, such as `GetLocalizedMessage` to the REST API technical profile. This is a long shot because I believe if the REST API returns `409 Conflict`, then the control flow is "interrupted" and the output claims transformation won't be invoked. – Chris Padgett Apr 08 '20 at 23:11
  • We tried this way, but like you said, the flow is interrupted and nothing gets called after the REST API call fails. – vronzeur Apr 15 '20 at 14:41

1 Answers1

0

We managed to find a workaround to this, so I'm posting this here for anyone else who might be interested in this.

Our restriction for localizations was the fact that used Phrase to manage our translations and wanted the CustomPolicy specific translations all in one place. Our CD workflow was as follows:

PolicyCommit -> Build Variable Replacement through PS -> Release Variable Replacement and localized strings replacement through PS & Policy Uploads

Barring the policy from localizing the APIs response, we had the following options to achieve this:

  1. Sending the language to the API and having the API return the appropriate error message in the appropriate language. We were reluctant to follow this because of a multitude of reasons, but mostly because we would also have to handle different regions, etc. in the API - something the policy does by itself.

  2. We actually had only one API that we called, and also only two error messages that were used. Hence we created an enum with the two error messages that would be localized. We then used a chain of InputClaimsTransformations that did the following:

Repeat Steps 1 through 3 for all the errors

1. CreateStringClaim (Create ClaimTypes for each of the error codes, holding the index of the error code in the enum)

2. GetMappedValueFromLocalizedCollection (Make the localized enum choose and hold the value of the required error code)

3. AddItemToStringCollection (Add the localized error from the enum to a StringCollection)

4. GenerateJson (Add the error codes StringCollection to the JSON payload to be sent to the API)

This way, the policy performed the localization for all the errors and we sent them along with the request to the API. The API, when an error occurred, picked one of the error messages from the policy and sent it back. This method was for us, because of our CD structure and Phrase integration, much easier than actually having the translations in a file hosted on the cloud to be accessed by the API.

Hope this helps someone; I can also add code in case someone needs it :)

vronzeur
  • 33
  • 6