| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 374a6c4 commit c15ba3b
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -654,6 +654,10 @@ def http_request(self, verb, path, query_data={}, post_data={}, | |||
| 654 | 654 | if 200 <= result.status_code < 300: | |
| 655 | 655 | return result | |
| 656 | 656 | ||
| 657 | + if result.status_code == 401: | ||
| 658 | + raise GitlabAuthenticationError(response_code=result.status_code, | ||
| 659 | + error_message=result.content) | ||
| 660 | + | ||
| 657 | 661 | raise GitlabHttpError(response_code=result.status_code, | |
| 658 | 662 | error_message=result.content) | |
| 659 | 663 | ||
@@ -674,7 +678,7 @@ def http_get(self, path, query_data={}, streamed=False, **kwargs): | |||
| 674 | 678 | ||
| 675 | 679 | Raises: | |
| 676 | 680 | GitlabHttpError: When the return code is not 2xx | |
| 677 | - GitlabParsingError: IF the json data could not be parsed | ||
| 681 | + GitlabParsingError: If the json data could not be parsed | ||
| 678 | 682 | """ | |
| 679 | 683 | result = self.http_request('get', path, query_data=query_data, | |
| 680 | 684 | streamed=streamed, **kwargs) | |
@@ -706,7 +710,7 @@ def http_list(self, path, query_data={}, **kwargs): | |||
| 706 | 710 | ||
| 707 | 711 | Raises: | |
| 708 | 712 | GitlabHttpError: When the return code is not 2xx | |
| 709 | - GitlabParsingError: IF the json data could not be parsed | ||
| 713 | + GitlabParsingError: If the json data could not be parsed | ||
| 710 | 714 | """ | |
| 711 | 715 | url = self._build_url(path) | |
| 712 | 716 | get_all = kwargs.pop('all', False) | |
@@ -726,19 +730,21 @@ def http_post(self, path, query_data={}, post_data={}, **kwargs): | |||
| 726 | 730 | ||
| 727 | 731 | Returns: | |
| 728 | 732 | The parsed json returned by the server if json is return, else the | |
| 729 | - raw content. | ||
| 733 | + raw content | ||
| 730 | 734 | ||
| 731 | 735 | Raises: | |
| 732 | 736 | GitlabHttpError: When the return code is not 2xx | |
| 733 | - GitlabParsingError: IF the json data could not be parsed | ||
| 737 | + GitlabParsingError: If the json data could not be parsed | ||
| 734 | 738 | """ | |
| 735 | 739 | result = self.http_request('post', path, query_data=query_data, | |
| 736 | 740 | post_data=post_data, **kwargs) | |
| 737 | 741 | try: | |
| 738 | - return result.json() | ||
| 742 | + if result.headers.get('Content-Type', None) == 'application/json': | ||
| 743 | + return result.json() | ||
| 739 | 744 | except Exception: | |
| 740 | 745 | raise GitlabParsingError( | |
| 741 | 746 | error_message="Failed to parse the server message") | |
| 747 | + return result | ||
| 742 | 748 | ||
| 743 | 749 | def http_put(self, path, query_data={}, post_data={}, **kwargs): | |
| 744 | 750 | """Make a PUT request to the Gitlab server. | |
@@ -756,7 +762,7 @@ def http_put(self, path, query_data={}, post_data={}, **kwargs): | |||
| 756 | 762 | ||
| 757 | 763 | Raises: | |
| 758 | 764 | GitlabHttpError: When the return code is not 2xx | |
| 759 | - GitlabParsingError: IF the json data could not be parsed | ||
| 765 | + GitlabParsingError: If the json data could not be parsed | ||
| 760 | 766 | """ | |
| 761 | 767 | result = self.http_request('put', path, query_data=query_data, | |
| 762 | 768 | post_data=post_data, **kwargs) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -210,3 +210,23 @@ class to raise. Should be inherited from GitLabError | |||
| 210 | 210 | raise error(error_message=message, | |
| 211 | 211 | response_code=response.status_code, | |
| 212 | 212 | response_body=response.content) | |
| 213 | + | ||
| 214 | + | ||
| 215 | + def on_http_error(error): | ||
| 216 | + """Manage GitlabHttpError exceptions. | ||
| 217 | + | ||
| 218 | + This decorator function can be used to catch GitlabHttpError exceptions | ||
| 219 | + raise specialized exceptions instead. | ||
| 220 | + | ||
| 221 | + Args: | ||
| 222 | + error(Exception): The exception type to raise -- must inherit from | ||
| 223 | + GitlabError | ||
| 224 | + """ | ||
| 225 | + def wrap(f): | ||
| 226 | + def wrapped_f(*args, **kwargs): | ||
| 227 | + try: | ||
| 228 | + return f(*args, **kwargs) | ||
| 229 | + except GitlabHttpError as e: | ||
| 230 | + raise error(e.response_code, e.error_message) | ||
| 231 | + return wrapped_f | ||
| 232 | + return wrap | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments