Introduction
Recently, I was working with a REST API where I needed to register a user account. I decided to reuse the core user registration REST API, however, the only problem was that the error messages were not user-friendly.
At that time, I felt the need to alter the exception messages thrown by the core REST API. I got the opportunity to explore the REST API workflow and discovered the 'Event Subscriber' approach that could be used.
Example
Let’s take a very generic scenario: a user tries to create an account with an email ID that already exists. When the REST API endpoint is hit at the backend, a more user-friendly exception message is thrown from the core user registration API using the 'Event Subscriber' approach through customization.
Default core user registers Rest API thrown error message:
"message": "Unprocessable Entity: validation failed.\nmail: email address nia@gmail.com is already taken.\n"s
Custom thrown error message for core user register REST API: By using event subscriber approach: see attached screencast for demo:
"message": "The email address nia@gmail.com is already taken."
![Custom thrown error message](https://cdn.prod.website-files.com/6470768de8327f36a7ae11a5/6687c7918a4bd163349691c2_gif-ezgif.com-gif-to-webp-converter.webp)
Steps to alter error messages thrown from core REST API
Steps to alter the error message thrown from core REST API by using the “event subscriber” approach:
1. Create a new service: “custom_rest_validation.custom_rest_validation_subscriber” in which tags key pass event_subscriber & class key pass name of the class that needs to call when this specified event subscriber calls.
![Creation of new service](https://cdn.prod.website-files.com/6470768de8327f36a7ae11a5/6687bbf28e9cc2e96f7b1181_AD_4nXdEVwk9fa9UQPkFXmdQSawpPKfCoUslPhmA_4n4cI9NS7sU0lcVNsPk_I0wF0C6DalTggr3YA0wiQ9-zbsQ2Z2C_ZQgz8M3Nqizgh_kOCg-eMTuas3r_FYI1yx8Lanq7K64Bhsx9vOKQdMDBjR1PCgfvnx0.png)
Note: Services are defined in a file called 'custom_rest_validation
.services.yml', assuming that your module is named 'custom_rest_validation. This file should be located in the root directory of your module project.
2. Create an event subscriber class that is attached to the newly created service in the above-mentioned step.
![Creation of an event subscriber class](https://cdn.prod.website-files.com/6470768de8327f36a7ae11a5/6687bbf14194cdfda9d0631b_AD_4nXeA5AoaaJ3NPjZefpADw2NWqXX4eSKZqOfdv3GRs1nRcSrSZYyO7PWSRhWsLSyotPv4RtMgMbJYmZuZXcK6yv0m5iEE6ax19nUC3FqDMxh9T6AOxiCzy_558xIj4YxwvOEwVsn2lR-HZfGZkZd39QpJNUw.png)
Here, getPriority() handles the exceptions we want for JSON calls before core subscribers (with priority -70/-75). Therefore, it is given a higher priority value (-69) than the core subscriber.
![getPriority() handleing the exceptions](https://cdn.prod.website-files.com/6470768de8327f36a7ae11a5/6687c897454cc80d798d4378_AD_4nXcBXx8q8dQpqpVzD4XTfi79iLyEJqANmAicUVtC9h2Uzv9UZMDZz7ZNZcML3rIrJxBLAbIp3dgunAN8VSbjZdvC4dLvKH7icW3WdlUp6lkqiF5GHca3iCj9JfU1FSuj_eBA2phSGdL7iwsdNdGnixLI6HA.png)
Here, onException() handles errors for this subscriber and is used to throw custom exception messages. A condition is added so that the custom exception is thrown only for the user registration API and for the email field.
![onException() handling errors](https://cdn.prod.website-files.com/6470768de8327f36a7ae11a5/6687bbf273e4fd81a5e355f7_AD_4nXdNRAKDFw66X3nGB-Ym95Vasj1hgXHiBs81U1bPITxTkoSYXIHujn_DwchxD_obFkf7f7GSdqwJHLaab48Nb0OSkwo57jCw6ALmXHkx0zkrND8vIxgA6Nw0nn6w0mOKM5l8c6wBZVy9OiHxA7HKz7g96bQU.png)
Here call the custom parseErrorMsesage($msg) conditionally, which returns a human-readable user-friendly error message “The email address example@gmail.com is already taken.” if the user enters an existing email while creating a new user account.
![Human-readable user-friendly error message](https://cdn.prod.website-files.com/6470768de8327f36a7ae11a5/6687bbf1da59e7c6c2802190_AD_4nXdtqeAbudlnFu7s_LRi9xBfAmeaO-Ru48LcbkYlDK40_WFOmq6kTdLkwBwX9OUgSfAjoMMjT4E1tosstUHAxPABz-muNzqR7UTVbHdVKQ38D4QgBzbrExI9Jn1Lo5bOcjyY5TlZogULxVLqTjA57JVsVVk.png)
Note: The event subscriber class CustomExceptionSubscriber.php will be placed in the following folder structure: root_folder_module/src/EventSubscriber/.
We can alter the exception message of the core REST API by extending an "event subscribe" approach.
Conclusion
This approach allows you to utilize the core REST API implementation and integrate desired functionality to provide more human-readable, user-friendly exception messages to end users, thereby enhancing the user experience. The following actions are implemented using the REST API:
- Get data from the endpoint (GET request)
- Create data at the endpoint (POST request)
- Update data at the endpoint (PATCH request)
- Delete data from the endpoint (DELETE request)
Reference links
For more details about the REST API, you can follow the GitHub demo link. For example, in this demo, I have altered the error message of the core user registration API. If a user enters an existing email ID, a custom exception message will be thrown instead of the default error message from the core REST API for user registration.
Rest API: documentation