How to return a json response from a flask error handler?

Solution
import json

from flask import Flask, Response, abort

app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True

#register 500 error handler
@app.errorhandler(Exception)
# handle all other exception
def all_exception_handler(error):
    res = {"error": str(error)}
    return Response(status=500, mimetype="application/json", response=json.dumps(res))


# handle 401 exception
def error_401_handler(error):
    res = {"error": "Unauthorized"}
    return Response(status=401, mimetype="application/json", response=json.dumps(res))


# test exception 500 with http get
@app.route("/test500")
def test500():
    raise Exception("test exception")


# test exception 401 with http get
@app.route("/test401")
def test401():
    abort(401)


# register 401 error handler
app.register_error_handler(401, error_401_handler)

app.run(host="0.0.0.0")