from flask import Flask, abort
app = Flask(__name__)
@app.errorhandler(500) # 1. 通过http状态码捕获异常信息
def internal_server_error(e):
return '500: 拜拜了,您咧!'
@app.errorhandler(ZeroDivisionError) # 通过异常类进行异常捕获
def zero_division_error(e):
return '除数不能为零 zero'
class APIError(Exception): # 捕获系统异常或者自定义异常
pass
@app.errorhandler(APIError)
def error_api(e):
return "错误: %s" % e
@app.route('/')
def qwq():
# abort(500) # 调用internal_server_error
# a = 2/0 # 调用zero_division_error
raise APIError('api接口调用参数有误!') # 调用error_apierror
return '200: ok'
if __name__ == '__main__':
app.run(debug=True)