【问题标题】:How to change Prometheus error message if token is invalid?如果令牌无效,如何更改 Prometheus 错误消息?
【发布时间】:2021-09-25 14:02:27
【问题描述】:

我有一个名为 main.py 的 python 文件,我正在尝试使用特定令牌建立 Prometheus 连接。但是,如果令牌已过期,而不是打印出 prometheus_api_client.exceptions.PrometheusApiClientException 的错误消息,我怎样才能使用 tryexcept 块打印我们喜欢的 status_code: 500, reason: Invalid token 的错误消息。

代码

#token="V0aksn-as9ckcnblqc6bi3ans9cj1nsk" #example, expired token
token ="c0ams7bnskd9dk1ndk7aKNYTVOVRBajs" #example, valid token
pc = PrometheusConnect(url = url, headers={"Authorization": "bearer {}".format(token)}, disable_ssl=True)
try:
#Not entirely sure what to put here and the except block
except:

我已经在 tryexcept 块中测试了几个代码,但无法摆脱来自 Prometheus 的长错误。有什么建议吗?

【问题讨论】:

    标签: python prometheus


    【解决方案1】:

    如何将您的 pc 变量放入 try 中,并将 PrometheusApiClientException 放入异常中。如果这不起作用,请转到源文件并使用开发人员在进行授权时使用的任何异常。

    【讨论】:

      【解决方案2】:

      这就是您在 try/except 块中捕获该异常的方式

      try:
          # Interact with prometheus here
          pass
      except prometheus_api_client.exceptions.PrometheusApiClientException as e:
          print('status_code: 500, reason: Invalid token')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-29
        相关资源
        最近更新 更多