Prevent crashing on PermissionError when importing http.server (#7095)

* Fix PermissionError when importing http.server

Instead of crashing, now it'll just not start the server. This means that you won't be able to receive the response from the log-in so you won't be able to log in. The browser gives an error that it can't find the page on localhost. But at least it doesn't crash.

Fixes crash CURA-3Q.

* Log an error when the HTTP server can't be started

Contributes to crash CURA-3Q.

* Indicate that types are optional

Attempt to fix the typing issue with MyPy. I can't run this locally so the CI server will have to tell me if this fixed it.

Contributes to Sentry issue CURA-3Q.
This commit is contained in:
Ghostkeeper 2020-02-17 17:01:11 +01:00 committed by GitHub
parent 769fc8fd37
commit e52dc56a64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,13 +1,18 @@
# Copyright (c) 2019 Ultimaker B.V.
# Copyright (c) 2020 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
import threading
from typing import Optional, Callable, Any, TYPE_CHECKING
from typing import Any, Callable, Optional, TYPE_CHECKING
from UM.Logger import Logger
from cura.OAuth2.AuthorizationRequestServer import AuthorizationRequestServer
from cura.OAuth2.AuthorizationRequestHandler import AuthorizationRequestHandler
got_server_type = False
try:
from cura.OAuth2.AuthorizationRequestServer import AuthorizationRequestServer
from cura.OAuth2.AuthorizationRequestHandler import AuthorizationRequestHandler
got_server_type = True
except PermissionError: # Bug in http.server: Can't access MIME types. This will prevent the user from logging in. See Sentry bug Cura-3Q.
Logger.error("Can't start a server due to a PermissionError when starting the http.server.")
if TYPE_CHECKING:
from cura.OAuth2.Models import AuthenticationResponse
@ -50,6 +55,7 @@ class LocalAuthorizationServer:
Logger.log("d", "Starting local web server to handle authorization callback on port %s", self._web_server_port)
# Create the server and inject the callback and code.
if got_server_type:
self._web_server = AuthorizationRequestServer(("0.0.0.0", self._web_server_port), AuthorizationRequestHandler)
self._web_server.setAuthorizationHelpers(self._auth_helpers)
self._web_server.setAuthorizationCallback(self._auth_state_changed_callback)