Browse Source

add error handling

kunickyd 3 years ago
parent
commit
f8002df3e5
1 changed files with 19 additions and 0 deletions
  1. 19 0
      app.py

+ 19 - 0
app.py

@@ -1,5 +1,8 @@
+import grp
 from flask import Flask, request, jsonify
 from flask import Flask, request, jsonify
 from json import JSONEncoder
 from json import JSONEncoder
+import json
+import grpc
 
 
 # Here are Iroha dependencies.
 # Here are Iroha dependencies.
 # Python library generally consists of 3 parts:
 # Python library generally consists of 3 parts:
@@ -9,6 +12,7 @@ import binascii
 from iroha import IrohaCrypto
 from iroha import IrohaCrypto
 from iroha import Iroha, IrohaGrpc
 from iroha import Iroha, IrohaGrpc
 from werkzeug.wrappers import response
 from werkzeug.wrappers import response
+from werkzeug.exceptions import HTTPException
 
 
 # Here is the information about the environment and admin account information:
 # Here is the information about the environment and admin account information:
 IROHA_HOST_ADDR = os.getenv('IROHA_HOST_ADDR', '127.0.0.1')
 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)")
     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'])
 @app.route("/", methods=['GET'])
 def Hello():
 def Hello():
     return "IROHA REST API"
     return "IROHA REST API"