Kaynağa Gözat

添加错误类型检测

世祺 1 yıl önce
ebeveyn
işleme
4897b23014
2 değiştirilmiş dosya ile 82 ekleme ve 1 silme
  1. 15 0
      src/main.py
  2. 67 1
      测试.ipynb

+ 15 - 0
src/main.py

@@ -76,35 +76,50 @@ def delivery_from_grpc(delivery_entity: DeliveryEntity):
     }
 
 
+def check_error(res, context):
+    if not res.get('success', True):
+        match res.get('errorType'):
+            case 'validation':
+                context.abort(grpc.StatusCode.INVALID_ARGUMENT, json.dumps(res.get('errors')))
+            case _:
+                context.abort(grpc.StatusCode.UNKNOWN, json.dumps(res))
+
+
 class DeliveryServiceServicer(llnw_pb2_grpc.DeliveryServiceServicer):
 
     def ListDelivery(self, request, context):
         res = get_cl(request.base).list_delivery_service_instances(request.shortName).json()
+        check_error(res, context)
         return DeliveryList(deliveries=[delivery_to_grpc(results) for results in res['results']])
 
     def ValidateDelivery(self, request, context):
         delivery_config = delivery_from_grpc(request.delivery)
         del delivery_config['uuid']
         res = get_cl(request.base).validate_delivery_service_instance(delivery_config).json()
+        check_error(res, context)
         return delivery_to_grpc(res)
 
     def CreateDelivery(self, request, context):
         delivery_config = delivery_from_grpc(request.delivery)
         del delivery_config['uuid']
         res = get_cl(request.base).create_delivery_service_instance(delivery_config).json()
+        check_error(res, context)
         return delivery_to_grpc(res)
 
     def GetDelivery(self, request, context):
         res = get_cl(request.base).get_delivery_service_instance(request.id).json()
+        check_error(res, context)
         return delivery_to_grpc(res)
 
     def UpdateDelivery(self, request, context):
         delivery_config = delivery_from_grpc(request.delivery)
         res = get_cl(request.base).update_delivery_service_instance(delivery_config['uuid'], delivery_config).json()
+        check_error(res, context)
         return delivery_to_grpc(res)
 
     def DeleteDelivery(self, request, context):
         res = get_cl(request.base).delete_delivery_service_instance(request.id).json()
+        check_error(res, context)
         return delivery_to_grpc(res)
 
 

Dosya farkı çok büyük olduğundan ihmal edildi
+ 67 - 1
测试.ipynb