From b2855e0416074bdcbff1509e973206c6e3211178 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 17 Dec 2017 13:42:46 +0100 Subject: [PATCH] Created Error Handling (markdown) --- Error-Handling.md | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Error-Handling.md diff --git a/Error-Handling.md b/Error-Handling.md new file mode 100644 index 0000000..85eb067 --- /dev/null +++ b/Error-Handling.md @@ -0,0 +1,50 @@ +Errors are inevitable when working with the API, and they must be correctly handled by the use of `try..except` blocks. + +There are many errors that Telegram servers could return, but they all fall in one of these five categories (which are in turn children of the `Error` superclass): + +- `(400) BadRequest` +- `(420) Flood` +- `(500) InternalServerError` +- `(303) SeeOther` +- `(401) Unauthorized` + +As stated above, there are really many (too many) errors, and in case Pyrogram does not know anything about a specific error, it raises a special `(520) UnknownError` exception and logs it in the `unknown_errors.txt` file. Users are invited to report these unknown errors; in later versions of Pyrogram some kind of automatic error reporting module might be implemented. + +## Examples + +``` python +from pyrogram.api.errors import ( + BadRequest, Flood, InternalServerError, + SeeOther, Unauthorized, UnknownError +) + +try: + # Something + pass +except BadRequest: + pass +except Flood: + pass +except InternalServerError: + pass +except SeeOther: + pass +except Unauthorized: + pass +except UnknownError: + pass +``` + +Exceptions may also contain some informative values which can be useful. e.g. `FloodWait` holds the amount of seconds you have to wait before you can try again. The value is always stored in the `x` field of the returned exception object: + +``` python +from pyrogram.api.errors import FloodWait + +try: + # something + pass +except FloodWait as e: + print(e.x) +``` + +**TODO: Better explanation on how to deal with exceptions** \ No newline at end of file