I have a backend with golang that talks to k8s. I want to reformulate the error response that i get from k8s and send it to the frontend.
I want to return a meaningful validation error messages for the user, when he add a non valid name, something already exist ...
And i want something generic not hardcoded in each endpoint's controller.
I am using kubernetes/client-go.
- First error:
For example lets say i want to add a hotel to the etcd, when i try to add the hotel's name: hotel123, that's already exist.
- I get this error message:
\"hotel123\" already exists. - What i want :
hotel123 already exists.
- second error:
For example lets say i want to add a hotel to the etcd, when i try to add the hotel name: hotel_123, that's alerady exist.
- I get this error message:
\"hotel_123\" is invalid, Invalid value: \"hotel_123\"... - What i want:
hotel_123 is invalid
How to return a custom user friendly error message ?
PS: i have multiple functions, so the validation should be generic.
In general (although there are workarounds), if you want to trap an error in order to return a more useful error, you want to ensure the following conditions are met:
In the following example I'm trying to read a config file that doesn't exist. My code checks that the error returned is a
fs.PathErrorand then throws it's own more useful error. You can extend this general idea to your use case.In your debugging, you will find the
%Tformatter useful.For your specific use-case, you can use a Regex to parse out the desired text.
The regex below says:
^\W*start with any non-alhpanumeric characters(\w+)capture the alphanumeric string following\W*\s?match non-alphanumeric characters(is\sinvalid)capture "is invalid"As seen on this playground:
https://goplay.tools/snippet/bcZO7wa8Vnl