Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

flask return error response

@app.route('/')
def index():
    resp = make_response("Record not found", 400)
    resp.headers['X-Something'] = 'A value'
    return resp
Comment

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 :: delete all elements in list python 
Python :: distance matrix in python 
Python :: label point matplotlib 
Python :: pandas dataframe unique multiple columns 
Python :: how to change plot size in matplotlib 
Python :: exit python terminal 
Python :: python get file name without dir 
Python :: magic methods python 
Python :: how to custom page not found in django 
Python :: find length of text file python 
Python :: python telethon 
Python :: self-xss meaning 
Python :: python pynput space 
Python :: Action based permissions in Django Rest V3+ 
Python :: how to rotate screen with python 
Python :: dataframe column in list 
Python :: count of datatypes in columns 
Python :: extract tgz files in python 
Python :: python cross validation 
Python :: python time function in for loop 
Python :: ImportError: dynamic module does not define module export function 
Python :: sort a list of array python 
Python :: how to get the current line number in python 
Python :: pandas return specific row 
Python :: add one day to datetime 
Python :: combination 
Python :: how to get key of a particular value in dictionary python using index 
Python :: mailchimp send email python 
Python :: create array with unknown size in python 
Python :: remove substring from string python 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =