“Kesalahan Flask” Kode Jawaban

Kesalahan 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
Timmy44

respons kesalahan pengembalian labu

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

Penanganan kesalahan labu

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
Ugly Unicorn

Jawaban yang mirip dengan “Kesalahan Flask”

Pertanyaan yang mirip dengan “Kesalahan Flask”

Lebih banyak jawaban terkait untuk “Kesalahan Flask” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya