浏览代码

add error handling

kunickyd 3 年之前
父节点
当前提交
f8002df3e5
共有 1 个文件被更改,包括 19 次插入0 次删除
  1. 19 0
      app.py

+ 19 - 0
app.py

@@ -1,5 +1,8 @@
+import grp
 from flask import Flask, request, jsonify
 from json import JSONEncoder
+import json
+import grpc
 
 # Here are Iroha dependencies.
 # Python library generally consists of 3 parts:
@@ -9,6 +12,7 @@ import binascii
 from iroha import IrohaCrypto
 from iroha import Iroha, IrohaGrpc
 from werkzeug.wrappers import response
+from werkzeug.exceptions import HTTPException
 
 # Here is the information about the environment and admin account information:
 IROHA_HOST_ADDR = os.getenv('IROHA_HOST_ADDR', '127.0.0.1')
@@ -65,6 +69,21 @@ transferAssetsValidationErrors = [
     StatefulValidationError("Too long description", "Too long description", "Ensure that description length matches the criteria above (or just shorten it)")
 ]    
 
+@app.errorhandler(Exception)
+def handle_exception(e):
+    # pass through HTTP errors
+    if isinstance(e, HTTPException):
+        return e
+
+    if isinstance(e, grpc.RpcError):
+        return { 
+            "details" :  e.details(),           
+            "debug_error" : json.loads(e.debug_error_string())
+        }, 500
+
+    # now you're handling non-HTTP exceptions only
+    return e, 500
+
 @app.route("/", methods=['GET'])
 def Hello():
     return "IROHA REST API"