I'm using a ISelectionFactory to generate a list of values for a property - the data for which comes from an external service.
I'd like to present a meaning error message wihtin the CMS if the service throws an exception.
I've tried the following inside my GetSelections method:
throw new EPiServerException("The service has returned no values - please raise with your administrator.");
This is close as EPiServer doesn't render the block properties and I get the familiar red icon in the top right. The problem is the message is always:
Something went wrong
Is there anyway to present a custom message?
I tried several of the overloads but its always the above message.
Forgive the rubbish spelling in that title!
Implement IValidate instead on page that also checks your selection factory and shows warning?
But IValidate is only used when saving a page or block - in my situation, the SelectOne property is required, so is shown as part of the Required Properties interface but if the service fails, the drop-down is unpopulated.
Rather than leave the editor with a form they now can't submit - I'd prefer to try and get a message on screen to indicate the service failure.
Do the results of the service change so often you need to call the service to populate the dropdown every time the page is loaded in edit mode?
If not so, I'd rather have the values for the dropdown synced from the service to Episerver (as a property somewhere) in a scheduled job as often as needed and then just read from that property instead of calling the service.
Not really an answer to your question, but it seems a bit of an overhead to call the service everytime. (Though I don't know the whole use case you are having).
To proceed with the error itself, I'd suggest you look into where the error comes from and if you can workaround it and if you can override the widget that displays the dropdown to show your custom message. This might help you with that.
I am caching the response from the service so subsequent calls don't take the hit. I'm just trying to manage an edge case.
I'll take a look at your article.
Tested a little by tweaking IValidate implementation but it got too ugly in the end. Displaying by modifying the property like Marija or directly on page (just for editors) seems to be the easiest path...
How about letting the ISelectionFactory populate the dropdown with a single row, where the value is something unique to be identified as "something went wrong".
Create a validationattribute that is added to that same property and if the value is the unique identifier from above, validate to false and let the error message be something understandable.
Here are some useful hints on validation.
I suggest to catch the error, return a SelectItem object an with a special value e.g. -1 , and then use IValidate to check the selection value is -1 to return your custom error message (e.g. Service is not available)