简体   繁体   English

自定义异常未在 Python Flask 应用程序中捕获

[英]Custom Exception not caught in Python Flask App

I got a Flask app calling different modules.我有一个调用不同模块的 Flask 应用程序。 One of them eventually raises a NoPrice exception but the except NoPrice doesn't catch it as a NoPrice but except Exception catches it...其中一个最终引发了一个 NoPrice 异常,但except NoPrice没有将它作为 NoPrice 捕获,但except Exception捕获它......

class NoPrice(Exception):
    pass

somewhere in a module模块中的某处

raise NoPrice('error')

in my flask app在我的烧瓶应用程序中

try:
    raise NoPrice('main')
except NoPrice as e:
    print('main')
except Exception as e:
    print('main2')

try:
    resp = alicia.create_predictions(select)
except NoPrice as e:
    print('main3')
except Exception as e:
    print('main4')
    print(repr(e))
    print(e)
    print(traceback.format_exc())

The output of this is这个的输出是

main
main4
NoPrice('No price found for material_reference 0148054b-e681-4fb6-9722-946f3cfa8529 the weight 1.7471266917293233', 'occurred at index 0')
('No price found for material_reference 0148054b-e681-4fb6-9722-946f3cfa8529 the weight 1.7471266917293233', 'occurred at index 0')
Traceback (most recent call last):
  File "c/main.py", line 71, in create_predictions_api
    resp = alicia.create_predictions(select)
  File "/absolutepath/app/alicia.py", line 703, in create_predictions
    self.set_machines_and_format()
  File "/absolutepath/app/alicia.py", line 574, in set_machines_and_format
    x["is_screenprinting_option"]), axis = 1)
  File "/absolutepath/venv/lib/python3.7/site-packages/pandas/core/frame.py", line 6913, in apply
    return op.get_result()
  File "/absolutepath/venv/lib/python3.7/site-packages/pandas/core/apply.py", line 186, in get_result
    return self.apply_standard()
  File "/absolutepath/venv/lib/python3.7/site-packages/pandas/core/apply.py", line 292, in apply_standard
    self.apply_series_generator()
  File "/absolutepath/venv/lib/python3.7/site-packages/pandas/core/apply.py", line 321, in apply_series_generator
    results[i] = self.f(v)
  File "/absolutepath/app/alicia.py", line 574, in <lambda>
    x["is_screenprinting_option"]), axis = 1)
  File "/absolutepath/app/alicia.py", line 359, in predict_price_relying_on_format
    output = Jerome(self.product, self.hipe_client_config).run(self.quote_input)
  File "/absolutepath/app/jerome/jerome.py", line 235, in run
    pliant = pliant_obj.price(quote_input, self.material_reference)
  File "/absolutepath/app/jerome/options_construction/pliant.py", line 66, in price
    quote_input['matter_margin_percentage'])
  File "/absolutepath/app/jerome/options_construction/pliant.py", line 52, in pliant
    raise NoPrice(f"No price found for material_reference {material_reference.id} the weight {x1}")
exceptions.NoPrice: ('No price found for material_reference 0148054b-e681-4fb6-9722-946f3cfa8529 the weight 1.7471266917293233', 'occurred at index 0')

Why doesn't the except NoPrice catch my exception ?为什么除了 NoPrice 没有捕捉到我的异常?

Shouldn't the ouput be输出不应该是

main
main3

My goal is ultimately to handle only my NoPrice exception我的目标是最终只处理我的 NoPrice 异常

my_function 不得引发 NoPrice 异常,因此它不会在“主要 3”部分中捕获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM