priya-zha / Software-Engineering-Project

0 stars 2 forks source link

Handling Invalid API call due to invalid files sent over api call #11

Closed Sekhar0799 closed 11 months ago

Sekhar0799 commented 11 months ago

Issue Description

Issue Type: Bug/Improvement

Current Behavior

When the API is called with improper request parameters for file input, it responds with a 500 Internal Server Error. However, it is more ideal for developers to receive a 400 Bad Request status code with a meaningful error message when the API call contains invalid file input.

Expected Behavior

The API should be updated to handle invalid file input and respond with a 400 status code (Bad Request) along with a proper error message that guides developers on what went wrong with their request.

Steps to Reproduce

To reproduce the issue:

  1. Make an API call with improper or invalid file input parameters.
  2. Observe that the API responds with a 500 Internal Server Error.

Suggested Solution

  1. Update the API's error handling mechanism to check for invalid file input parameters.
  2. If invalid file input is detected, return a 400 status code (Bad Request).
  3. Include a clear and informative error message in the response body to help developers understand what went wrong and how to correct their request.

Before fix:

image

After Fix

image
Sekhar0799 commented 11 months ago

Solution Explanation

In this solution, we address the issue where the API was responding with a 500 Internal Server Error when called with improper request parameters for file input. The goal is to provide a more developer-friendly experience by returning a 400 Bad Request status code along with a meaningful error message when the request contains invalid file input.

Key Changes

  1. We use a try and except block to capture any exceptions that may occur during the execution of the run function. These exceptions can include issues related to improper file input.

  2. In the except block, we handle the exception by returning a JSON response with a 400 status code (Bad Request). The response includes an error message that combines the string "Improper request" with the exception message (if available). This message helps developers understand that the issue is related to their request.

Impact

This solution improves the error handling of the API, making it more informative and user-friendly. Developers calling the API with invalid file input will now receive a clear error message indicating that their request was improper, along with a 400 Bad Request status code. This allows them to diagnose and correct the issue more effectively.

Code Snippet


try:
  result = run(source = os.path.join(app.config['UPLOAD_FOLDER'], filename))
  return jsonify({"result":open(result).read()})
except Exception as e:
  return jsonify({"Error": "Improper request"+str(e)}),400