Open In App

How to Handle Error Handling Scenarios in API Tests With Postman?

Last Updated : 11 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

API testing is an important part of modern software development, that ensures the APIs behave as expected under various conditions. Postman is a popular tool for API testing, offering a user-friendly interface and powerful features to test and automate your APIs.

One important thing of API testing is handling error scenarios effectively. This article will guide you through handling error scenarios in API tests with Postman.

How to Identify Errors?

Most of the time we will get to know about the error by the status code. And for error handling also we will try to target that only. But sometimes backend developer will send the customised error response which gives the status code 200 or 202 which defines no content found and based on that the other team will target the procedure against that. In the below image, we can see that both the status code and Error response indicate that an error occurred. Now this is the error handled by the server but sometimes we get exceptions that are not handled and as a Tester, we have to look into that matter as well.

How-to-find-Error---First
How to Find Error

For that we follow some steps for Error Handling in Postman:

  1. Capturing and Parsing Error Response
  2. Managing Response with Test
  3. Error Logging

By using this steps we can easily handle the Errors or Exceptions.

1. Capturing and Parsing Errors

After sending the request we can suspect that whether error occurred or not using status code as we discussed above. Also for this we can write the Tests. And if it is a error then we can also capture them using this tests. Also we can the response into the file using the feature given by the Postman.

Second---Save-Response
Save Response to file

For capturing the Errors we will write the Tests which will check that whether request gives error and also whether the response has Error.

pm.test("Status code is for Error", function () {
pm.response.to.not.be.oneOf([400, 401, 403, 404, 500]);
});

pm.test("Error message is correct", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.message).to.eql("Error Occurred");
});


We ca write the Tests in Scripts tag, in that we have two options Pre-req and Post-res. In Pre-req we write the test which are to be executed before Request and Post-res are the test which are to be executed after the request is made successfully. And their responses can be seen in the bottom window stated Test-Results.

Third---Test-for-Error-Capturing
Test for Error Capturing

2. Managing error responses with tests

In above test we have checked that whether the response contains "Error Occurred". But we can do more with this. Let say we have API of Login and in that user entered wrong Email and Password and For that response should contain the response stating "Invalid Credentials". And we can do that same just like the above script.

pm.test("Error message of Invalid Credentials", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.message).to.contains("Invalid Credential");
});


Similarly we can Create a test for the Exception using this contains and based on that we can see that which error occurred in the request.

3. Error Logging

As we have seen that we can store the response in the File. But using the tests we can also log them in the postman. In postman we can write CHAI test and that uses javascript as a language and which means that we can use console.log() to create a entry in logger. Also Postman has a Console to see the logger message.

Fourth---Console
Log the Response or Error

Best Practices for effective error handling

For error handling we should follow some basic best practices.

  1. The most important is to structure the errors. As we have seen first that how proper response view model. So always Provide a clear and a proper structure our Error.
  2. Each Error message describe the error completely. Sometimes error not written properly might create a confusions and the first step to solve the error is to understand.
  3. We should create a document for common errors. In which all the errors are explained properly. this is one the important aspect because this also gives the clear idea to non developers.
  4. And Last is to implement a Logging in to a software so that it can give clear idea about when the error occurred.

Can I modify the actual response body in Postman?

No, we cannot modify the actual response body that Postman displays in the response panel. However, we can manipulate and log a modified version of the response body in the "Tests" tab using JavaScript for validation and debugging purposes.


Next Article

Similar Reads