@@ -9,6 +9,7 @@ import random
99import re
1010import shlex
1111import signal
12+ import ssl
1213import subprocess
1314import sys
1415import traceback
@@ -20,7 +21,7 @@ from threading import Thread, current_thread, main_thread
2021from time import time , sleep
2122from typing import Any , Callable , Literal , TypeVar , Generic , cast
2223from urllib .request import Request , urlopen
23- from urllib .error import HTTPError
24+ from urllib .error import HTTPError , URLError
2425
2526BRANCH_PREFIX = "grok/"
2627PR_HEADER = "Pull Request"
@@ -1441,7 +1442,13 @@ class Main:
14411442 res = None
14421443 returncode = None
14431444 try :
1444- with urlopen (req ) as res :
1445+ # MacOS Python 3.10 doesn't seem to have OpenAI certs. It raises the
1446+ # error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed:
1447+ # unable to get local issuer certificate.
1448+ context = ssl .create_default_context ()
1449+ context .check_hostname = False
1450+ context .verify_mode = ssl .CERT_NONE
1451+ with urlopen (req , context = context ) as res :
14451452 res = json .load (res )
14461453 res = str (res ["choices" ][0 ]["message" ]["content" ].strip ())
14471454 parts = [part .strip () for part in res .split (AI_SEPARATOR )]
@@ -1455,6 +1462,9 @@ class Main:
14551462 res = e .read ().decode ()
14561463 returncode = e .code
14571464 raise UserException (f"{ e .code } { e .reason } : { res } " )
1465+ except URLError as e :
1466+ returncode = - 1
1467+ raise UserException (f"Network error: { e } " )
14581468 finally :
14591469 self .debug_log_shell_command (
14601470 cmd = ["curl" , "-X" , "POST" , req .full_url ],
0 commit comments