MasoniteFramework / exceptionite

A Python Exception Library. Designed to make handling and displaying exceptions a cinch.
MIT License
124 stars 4 forks source link

Add more details for exception #47

Closed flowercoder closed 2 years ago

flowercoder commented 2 years ago

I try this exception library yesterday,I have some trouble right now. I need to use request method to visit some website. I try this library, it only show one line error information about it if my visit does not work. I need specific exceptions about it. So please add a optional in this case. In addition, i try to “Adding Context” by the homepage example. I follow it, but it does not work. I don`t know why. So please offer more example about this library. Thanks a lot.

josephmancuso commented 2 years ago

I will need more information for this issue.

girardinsamuel commented 2 years ago

@flowercoder I don't know if you have seen the comment above 😉 ?

flowercoder commented 2 years ago

@flowercoder I don't know if you have seen the comment above 😉 ?

Sorry, I just saw it. Right now I mean I have no idea that my issue is really issue or not. Cause that I use the request method, the response back the 405 code, I just realized that i cannot look at the response code as error information.

girardinsamuel commented 2 years ago

@flowercoder As Joe was saying we will need a bit more info to help you on this:

And if you can:

flowercoder commented 2 years ago

I take a sample example(not good,I forget my pervious code) I use request method, and it try to get some response from some website.Actually, the website suddenly close the api ,and it return some information.the code and the error as listed below.

import requests
from exceptionite import Handler

try:
    url = "https://www.google.com"
    headers = {
                'Accept': 'application/json, text/plain, */*',
                'X-Signature': '',
                'X-Service-Method': 'getVerifyCodeInfo',
                'X-Access-Token': 'null',
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
                'X-Service-Id': 'cas-web.smsService',
                'Content-Type': 'application/json',
                'host': 'e-hospital.jshtcm.com'
            }
    response = requests.post(url, headers)
    print(response.text)
except Exception as e:
    handler = Handler()
    handler.start(e)
    print("===========")
    print(e)

the response is listed below

/Users/xxx/PycharmProjects/flaskProject/venv/bin/python
/Users/xxx/PycharmProjects/flaskProject/appointmentcookie.py
<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 405 (Method Not Allowed)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>405.</b> <ins>That’s an error.</ins>
  <p>The request method <code>POST</code> is inappropriate for the URL <code>/</code>.  <ins>That’s all we know.</ins>

About half month ago,I hope I can collect the response error like " Error 405 (Method Not Allowed)!!1 by the exceptionite.

girardinsamuel commented 2 years ago

Using requests python module does not raise an exception if the returned server response has an error. The response will be an error response with an error status code.

exceptionite display information about the error based on an exception. Here no exception is raised, so you exceptionite can't help you. What you should do is to "analyze"/"parse" the response object to find the status code, the error message...

url = "https://www.google.com"
    headers = {
                'Accept': 'application/json, text/plain, */*',
                'X-Signature': '',
                'X-Service-Method': 'getVerifyCodeInfo',
                'X-Access-Token': 'null',
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
                'X-Service-Id': 'cas-web.smsService',
                'Content-Type': 'application/json',
                'host': 'e-hospital.jshtcm.com'
            }
    response = requests.post(url, headers)
    code = response.status_code
    error_content = response.text
    # maybe if it's an api it's easier to parse the response as json if the server handles it
    error_content = response.json()

If you really want to use exceptionite you should parse the error response from requests module and then raise an exception with the info and handles it with exceptionite. It would look like:

import requests
# ...
response = requests.post(url, headers)
if response.status_code >= 400:
    # an error happened
    # analyze content
    error = response.json()
    error_title = ...
    message = f"Error {response.status_code} - {error_title}"
    exception = CustomException(message)
    handler = Handler()
    handler.start(exception)
    handler.render("terminal")
else:
    # continue normally...

But I am not sure exceptionite is very useful here.

girardinsamuel commented 2 years ago

I am closing this issue as we clarified how to use exceptionite in this case even if it has not really been made for this use case. 😉