Skip to content

Commit

Permalink
Add some more pretty errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aniezurawski authored Mar 12, 2021
1 parent fa34e77 commit 5bc391d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 20 deletions.
122 changes: 112 additions & 10 deletions neptune/api_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from neptune.exceptions import NeptuneException
from neptune import envs
from neptune.exceptions import NeptuneException, STYLES


class NeptuneApiException(NeptuneException):
Expand All @@ -33,32 +34,133 @@ def __init__(self):

class ServerError(NeptuneApiException):
def __init__(self):
super(ServerError, self).__init__('Server error. Please try again later.')
message = """
{h1}
----ServerError-----------------------------------------------------------------------
{end}
Neptune Client Library encountered an unexpected Server Error.
Please try again later or contact Neptune support.
"""
inputs = dict(list({}.items()) + list(STYLES.items()))
super(ServerError, self).__init__(message.format(**inputs))


class Unauthorized(NeptuneApiException):
def __init__(self):
super(Unauthorized, self).__init__('Your API token is invalid.')
message = """
{h1}
----Unauthorized-----------------------------------------------------------------------
{end}
You have no permission to access given resource.
- Verify your API token is correct.
See: https://docs.neptune.ai/security-and-privacy/api-tokens/how-to-find-and-set-neptune-api-token.html
- Verify if you set your Project qualified name correctly
The correct project qualified name should look like this {correct}WORKSPACE/PROJECT_NAME{end}.
It has two parts:
- {correct}WORKSPACE{end}: which can be your username or your organization name
- {correct}PROJECT_NAME{end}: which is the actual project name you chose
- Ask your organization administrator to grant you necessary privileges to the project
"""
inputs = dict(list({}.items()) + list(STYLES.items()))
super(Unauthorized, self).__init__(message.format(**inputs))


class Forbidden(NeptuneApiException):
def __init__(self):
super(Forbidden, self).__init__('You have no permissions to access this resource.')
message = """
{h1}
----Forbidden-----------------------------------------------------------------------
{end}
You have no permission to access given resource.
- Verify your API token is correct.
See: https://docs.neptune.ai/security-and-privacy/api-tokens/how-to-find-and-set-neptune-api-token.html
- Verify if you set your Project qualified name correctly
The correct project qualified name should look like this {correct}WORKSPACE/PROJECT_NAME{end}.
It has two parts:
- {correct}WORKSPACE{end}: which can be your username or your organization name
- {correct}PROJECT_NAME{end}: which is the actual project name you chose
- Ask your organization administrator to grant you necessary privileges to the project
"""
inputs = dict(list({}.items()) + list(STYLES.items()))
super(Forbidden, self).__init__(message.format(**inputs))


class InvalidApiKey(NeptuneApiException):
def __init__(self):
super(InvalidApiKey, self).__init__('The provided API key is invalid.')


class NamespaceNotFound(NeptuneApiException):
message = """
{h1}
----InvalidApiKey-----------------------------------------------------------------------
{end}
Your API token is invalid.
Learn how to get it in this docs page:
https://docs.neptune.ai/security-and-privacy/api-tokens/how-to-find-and-set-neptune-api-token.html
There are two options to add it:
- specify it in your code
- set an environment variable in your operating system.
{h2}CODE{end}
Pass the token to {bold}neptune.init(){end} via {bold}api_token{end} argument:
{python}neptune.init(project_qualified_name='WORKSPACE_NAME/PROJECT_NAME', api_token='YOUR_API_TOKEN'){end}
{h2}ENVIRONMENT VARIABLE{end} {correct}(Recommended option){end}
or export or set an environment variable depending on your operating system:
{correct}Linux/Unix{end}
In your terminal run:
{bash}export {env_api_token}=YOUR_API_TOKEN{end}
{correct}Windows{end}
In your CMD run:
{bash}set {env_api_token}=YOUR_API_TOKEN{end}
and skip the {bold}api_token{end} argument of {bold}neptune.init(){end}:
{python}neptune.init(project_qualified_name='WORKSPACE_NAME/PROJECT_NAME'){end}
You may also want to check the following docs pages:
- https://docs.neptune.ai/security-and-privacy/api-tokens/how-to-find-and-set-neptune-api-token.html
- https://docs.neptune.ai/getting-started/quick-starts/log_first_experiment.html
{correct}Need help?{end}-> https://docs.neptune.ai/getting-started/getting-help.html
"""
inputs = dict(list({'env_api_token': envs.API_TOKEN_ENV_NAME}.items()) + list(STYLES.items()))
super(InvalidApiKey, self).__init__(message.format(**inputs))


class WorkspaceNotFound(NeptuneApiException):
def __init__(self, namespace_name):
super(NamespaceNotFound, self).__init__("Namespace '{}' not found.".format(namespace_name))
message = """
{h1}
----WorkspaceNotFound-------------------------------------------------------------------------
{end}
Workspace {python}{workspace}{end} not found.
Workspace is your username or a name of your team organization.
"""
inputs = dict(list({'workspace': namespace_name}.items()) + list(STYLES.items()))
super(WorkspaceNotFound, self).__init__(message.format(**inputs))


class ProjectNotFound(NeptuneApiException):
def __init__(self, project_identifier):
super(ProjectNotFound, self).__init__("Project '{}' not found.".format(project_identifier))
message = """
{h1}
----ProjectNotFound-------------------------------------------------------------------------
{end}
Project {python}{project}{end} not found.
Verify if your project's name was not misspelled. You can find proper name after logging into Neptune UI: ui.neptune.ai.
"""
inputs = dict(list({'project': project_identifier}.items()) + list(STYLES.items()))
super(ProjectNotFound, self).__init__(message.format(**inputs))


class PathInProjectNotFound(NeptuneApiException):
Expand Down
23 changes: 15 additions & 8 deletions neptune/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ def __init__(self, path):
"File {} is not a valid notebook. Should end with .ipynb.".format(path))


class InvalidChannelX(NeptuneException):
def __init__(self, x):
super(InvalidChannelX, self).__init__(
"Invalid channel X-coordinate: '{}'. The sequence of X-coordinates must be strictly increasing.".format(x))


class NoChannelValue(NeptuneException):
def __init__(self):
super(NoChannelValue, self).__init__('No channel value provided.')
Expand Down Expand Up @@ -299,8 +293,21 @@ def __init__(self, app_url):

class CannotResolveHostname(NeptuneException):
def __init__(self, host):
super(CannotResolveHostname, self).__init__(
"Cannot resolve hostname {}. Please contact Neptune support.".format(host))
message = """
{h1}
----CannotResolveHostname-----------------------------------------------------------------------
{end}
Neptune Client Library was not able to resolve hostname {host}.
What should I do?
- Check if your computer is connected to the internet.
- Check if your computer should use any proxy to access internet.
If so, you may want to use {python}proxies{end} parameter of {python}neptune.init(){end} function.
See https://docs.neptune.ai/api-reference/neptune/index.html#neptune.init
and https://requests.readthedocs.io/en/master/user/advanced/#proxies
"""
inputs = dict(list({'host': host}.items()) + list(STYLES.items()))
super(CannotResolveHostname, self).__init__(message.format(**inputs))


class UnsupportedClientVersion(NeptuneException):
Expand Down
4 changes: 2 additions & 2 deletions neptune/internal/backends/hosted_neptune_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
ExperimentLimitReached,
ExperimentNotFound,
ExperimentValidationError,
NamespaceNotFound,
WorkspaceNotFound,
NotebookNotFound,
PathInProjectNotFound,
ProjectNotFound,
Expand Down Expand Up @@ -166,7 +166,7 @@ def get_projects(self, namespace):
).response()
return r.result.entries
except HTTPNotFound:
raise NamespaceNotFound(namespace_name=namespace)
raise WorkspaceNotFound(namespace_name=namespace)

@with_api_exceptions_handler
def get_project_members(self, project_identifier):
Expand Down

0 comments on commit 5bc391d

Please sign in to comment.