Skip to content

Commit f23c9c4

Browse files
authored
Merge pull request #398 from FoamyGuy/json_exception_handling
JSON response exception handling & update actions versions
2 parents 80adf57 + 0ad55fe commit f23c9c4

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

.github/workflows/bundle_cron.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
if: startswith(github.repository, 'adafruit/')
3030
steps:
3131
- name: Set up Python 3.12
32-
uses: actions/setup-python@v4
32+
uses: actions/setup-python@v5
3333
with:
3434
python-version: 3.12
3535
- name: Load contributor cache
@@ -40,7 +40,7 @@ jobs:
4040
- name: Versions
4141
run: |
4242
python3 --version
43-
- uses: actions/checkout@v3
43+
- uses: actions/checkout@v4
4444
with:
4545
submodules: true
4646
- name: Install deps

.github/workflows/learn_cron.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
# default branches).
2727
if: ${{ (github.repository_owner == 'adafruit') }}
2828
steps:
29-
- uses: actions/checkout@v3
29+
- uses: actions/checkout@v4
3030
with:
3131
repository: ${{ github.repository_owner }}/Adafruit_Learning_System_Guides
3232
token: ${{ secrets.ADABOT_GITHUB_ACCESS_TOKEN }}

.github/workflows/reports_cron.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ jobs:
3737
BIGQUERY_PRIVATE_KEY: ${{ secrets.BIGQUERY_PRIVATE_KEY }}
3838
BIGQUERY_CLIENT_EMAIL: ${{ secrets.BIGQUERY_CLIENT_EMAIL }}
3939
steps:
40-
- name: Set up Python 3.9
41-
uses: actions/setup-python@v4
40+
- name: Set up Python 3.11
41+
uses: actions/setup-python@v5
4242
with:
43-
python-version: 3.9
43+
python-version: 3.11
4444
- name: Versions
4545
run: |
4646
python3 --version
47-
- uses: actions/checkout@v3
47+
- uses: actions/checkout@v4
4848
with:
4949
submodules: true
5050
- name: Install deps

.github/workflows/test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ jobs:
2626
if: startswith(github.repository, 'adafruit/')
2727
steps:
2828
- name: Set up Python
29-
uses: actions/setup-python@v4
29+
uses: actions/setup-python@v5
3030
with:
3131
python-version: 3
3232
- name: Versions
3333
run: |
3434
python3 --version
35-
- uses: actions/checkout@v3
35+
- uses: actions/checkout@v4
3636
with:
3737
submodules: true
3838
- name: Install deps

adabot/github_requests.py

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
"""Wrapper for GitHub requests."""
66

7+
import types
78
from base64 import b64encode
89
import datetime
910
import functools
@@ -57,6 +58,23 @@ def _fix_kwargs(kwargs):
5758
return kwargs
5859

5960

61+
def _safe_response_json(self):
62+
"""
63+
overridden response.json() function that will catch JSONDecodeError
64+
log it but try to continue on afterward.
65+
"""
66+
try:
67+
return self.original_json()
68+
except requests.exceptions.JSONDecodeError:
69+
exception_text = traceback.format_exc()
70+
if "ADABOT_GITHUB_ACCESS_TOKEN" in os.environ:
71+
exception_text = exception_text.replace(
72+
os.environ["ADABOT_GITHUB_ACCESS_TOKEN"], "[secure]"
73+
)
74+
logging.warning("%s", exception_text)
75+
return {}
76+
77+
6078
def request(method, url, **kwargs):
6179
"""Processes request for `url`."""
6280
try:
@@ -107,6 +125,8 @@ def request(method, url, **kwargs):
107125
logging.info("Sleeping %s seconds", reset_diff.seconds + 60)
108126
time.sleep(reset_diff.seconds + 60)
109127

128+
response.original_json = response.json
129+
response.json = types.MethodType(_safe_response_json, response)
110130
return response
111131

112132

0 commit comments

Comments
 (0)