Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

error handling flask

from flask import Flask, abort
from auth import AuthError
# depending on the error either 400 or any
#just work with the following

@app.errorhandler(404)
def resource_not_found(error):
  return jsonify({
    "success":  True,
    "error": 404,
    "message": "Resource not found"
  }), 404
##This works for any type of status code error
#You'll follow the same steps just change the error value and message :)

### also for Auth error. 

@app.errorhandler(AuthError)
def AuthError(error):
  """Need to return JSON and we'll have to get a response""" 
  response = jsonify(error)
  response.status_code = error.status_code
  
  return response
Comment

flask error handling

from flask import json
from werkzeug.exceptions import HTTPException

@app.errorhandler(HTTPException)
def handle_exception(e):
    """Return JSON instead of HTML for HTTP errors."""
    # start with the correct headers and status code from the error
    response = e.get_response()
    # replace the body with JSON
    response.data = json.dumps({
        "code": e.code,
        "name": e.name,
        "description": e.description,
    })
    response.content_type = "application/json"
    return response
Comment

flask error handling

from werkzeug.exceptions import HTTPException

@app.errorhandler(Exception)
def handle_exception(e):
    # pass through HTTP errors
    if isinstance(e, HTTPException):
        return e

    # now you're handling non-HTTP exceptions only
    return render_template("500_generic.html", e=e), 500
Comment

flask error

from waitress import serve
# app.run(host='0.0.0.0', port=port) # <---- REMOVE THIS
# serve your flask app with waitress, instead of running it directly.
serve(app, host='0.0.0.0', port=port) # <---- ADD THIS
Comment

PREVIOUS NEXT
Code Example
Python :: how to take multiple line input in python 
Python :: replace list python 
Python :: move file python os 
Python :: how to create an array in python 
Python :: how to get any letter of a string python 
Python :: how to stop a program after 1 second in python 
Python :: add image pptx python 
Python :: planets python 
Python :: Program for length of the shortest word 
Python :: python list pop vs remove 
Python :: Upper letter list 
Python :: generate random integers in a range python 
Python :: Python Tkinter Message Widget 
Python :: play music pygame 
Python :: creating new column with dictionary 
Python :: how to call a random function in python 
Python :: most popular python libraries 
Python :: how to find unique values in list in python 
Python :: import tsv as dataframe python 
Python :: save screenshot of screen in pygame 
Python :: python beginner projects 
Python :: find value in dictionary python 
Python :: Print First 10 natural numbers using while loop 
Python :: tkinter label auto text wrap 
Python :: how to get value from set in python 
Python :: how to run fastapi with code python 
Python :: python return number of characters in string 
Python :: how to reset username and password in django admin 
Python :: how to find permutation of numbers in python 
Python :: python possible combinations 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =