|
99 | 99 | - Bulk request handling |
100 | 100 | - KwikAPI Client |
101 | 101 | - Authentication |
| 102 | +- Custom error codes and messages |
102 | 103 |
|
103 | 104 | ### Versioning support |
104 | 105 | Versioning support will be used if user wants different versions of functionality with slightly changed behaviour. |
@@ -643,7 +644,7 @@ api.register(Calc(), 'v1') |
643 | 644 | def make_app(): |
644 | 645 | return tornado.web.Application([ |
645 | 646 | (r'^/api/.*', RequestHandler, dict(api=api)), |
646 | | - ]) |
| 647 | + ]) |
647 | 648 | if __name__ == "__main__": |
648 | 649 | app = make_app() |
649 | 650 | app.listen(8818) |
@@ -722,6 +723,95 @@ auth = b'Bearer %s' % b'key' |
722 | 723 | headers['Authorization'] = auth |
723 | 724 | ``` |
724 | 725 |
|
| 726 | +### Custom error codes and messages |
| 727 | +KwikAPI supports error `codes` and `messages`. |
| 728 | + |
| 729 | +A global map of error messages and error codes are maintained across the KwikAPI. Every error code specifies a unique error message that is possible. |
| 730 | + |
| 731 | +**KwikAPI's default error code:** |
| 732 | + |
| 733 | +| Error | Code | |
| 734 | +| ---- | ---- | |
| 735 | +| Unknown / Internal Exception | 50000 | |
| 736 | +| Duplicate API function | 50001 | |
| 737 | +| Unknown API function | 50002 | |
| 738 | +| Protocol already exists | 50003 | |
| 739 | +| Unknown protocol | 50004 | |
| 740 | +| Unknown version | 50005 | |
| 741 | +| Unsupported type | 50006 | |
| 742 | +| Type not specified | 50007 | |
| 743 | +| Unknown version or namespace | 50008 | |
| 744 | +| Streaming not supported | 50009 | |
| 745 | +| Keyword argument error | 50010 | |
| 746 | +| Authentication error | 50011 | |
| 747 | +| Non-keyword arguments error | 50012 | |
| 748 | + |
| 749 | +Exceptions raised by API's return default error if not handled which is `50000` by default. |
| 750 | + |
| 751 | +#### Examples |
| 752 | +- Response with an error code and message: |
| 753 | + > URL:`https://www.example.com/addd?a=10&b=20` |
| 754 | +
|
| 755 | + ```json |
| 756 | + { |
| 757 | + "message": "Unknown API Function: \"addd\"", |
| 758 | + "code": 50002, |
| 759 | + "error": "[(www.example.com) UnknownAPIFunction]", |
| 760 | + "success": false |
| 761 | + } |
| 762 | + ``` |
| 763 | + |
| 764 | + To return custom error messages and code, developer must raise an exception object with attributes `message` and `code` in it. |
| 765 | + |
| 766 | +- Raising custom error message and error code |
| 767 | + ```python |
| 768 | + import tornado.web |
| 769 | + import tornado.ioloop |
| 770 | + |
| 771 | + from kwikapi import API |
| 772 | + from kwikapi.tornado import RequestHandler |
| 773 | + |
| 774 | + # custom exception |
| 775 | + class CalcError(Exception): |
| 776 | + def __init__(self, message="Input error", code=1101): |
| 777 | + self.message = message |
| 778 | + self.code = code |
| 779 | + |
| 780 | + # Core logic that you want to expose as a service |
| 781 | + class Calc(object): |
| 782 | + def divide(self, a: int, b: int) -> int: |
| 783 | + try: |
| 784 | + return a / b |
| 785 | + except: |
| 786 | + raise CalcError(message="b can't be zero") |
| 787 | + |
| 788 | + # Register BaseCalc with KwikAPI |
| 789 | + api = API() |
| 790 | + api.register(Calc(), 'v1') |
| 791 | + |
| 792 | + # Passing RequestHandler to the KwikAPI |
| 793 | + def make_app(): |
| 794 | + return tornado.web.Application([ |
| 795 | + (r'^/api/.*', RequestHandler, dict(api=api)), |
| 796 | + ]) |
| 797 | + |
| 798 | + # Starting the application |
| 799 | + if __name__ == "__main__": |
| 800 | + app = make_app() |
| 801 | + app.listen(8888) |
| 802 | + tornado.ioloop.IOLoop.current().start() |
| 803 | + ``` |
| 804 | + Response: |
| 805 | + > URL:`https://www.example.com/divide?a=10&b=0` |
| 806 | + |
| 807 | + ```json |
| 808 | + { |
| 809 | + "success": false, |
| 810 | + "message": "b can't be zero", |
| 811 | + "code": 1101, |
| 812 | + "error": "[(www.example.com) CalcError]" |
| 813 | + } |
| 814 | + ``` |
725 | 815 | ## Run test cases |
726 | 816 | ```bash |
727 | 817 | $ python3 -m doctest -v README.md |
|
0 commit comments