Skip to content

Commit

Permalink
Move custom exceptions in errors.py (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgayon authored Jun 7, 2018
1 parent 4087db9 commit 897201a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
22 changes: 5 additions & 17 deletions docker_explorer/de.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import os
import sys

from docker_explorer import errors
from docker_explorer.lib import aufs
from docker_explorer.lib import overlay

Expand All @@ -33,19 +34,6 @@
sys.stdout = codecs.getwriter('utf8')(sys.stdout)


class BadStorageException(Exception):
"""Raised when the Storage method detection failed."""

def __init__(self, message):
"""Constructor for a BadStorageException.
Args:
message (str): the error message.
"""
super(BadStorageException, self).__init__(message)
self.message = message


class DockerExplorer(object):
"""Main class for the DockerExplorer tool.
Expand All @@ -70,14 +58,14 @@ def DetectStorage(self):
http://jpetazzo.github.io/assets/2015-06-04-deep-dive-into-docker-storage-drivers.html#60
Raises:
BadStorageException: If the storage backend couldn't be detected.
errors.BadStorageException: If the storage backend couldn't be detected.
"""
if not os.path.isdir(self.docker_directory):
error_msg = (
err_message = (
'{0:s} is not a Docker directory\n'
'Please specify the Docker\'s directory path.\n'
'hint: de.py -r /var/lib/docker').format(self.docker_directory)
raise BadStorageException(error_msg)
raise errors.BadStorageException(err_message)

if os.path.isfile(
os.path.join(self.docker_directory, 'repositories-aufs')):
Expand All @@ -99,7 +87,7 @@ def DetectStorage(self):
'Make sure the docker directory ({0:s}) is correct. '
'If it is correct, you might want to run this script'
' with higher privileges.'.format(self.docker_directory))
raise BadStorageException(err_message)
raise errors.BadStorageException(err_message)

def AddBasicOptions(self, argument_parser):
"""Adds the global options to the argument_parser.
Expand Down
38 changes: 38 additions & 0 deletions docker_explorer/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Custom exceptions."""

from __future__ import print_function, unicode_literals


class DockerExplorerError(Exception):
"""Base class for DockerExplorer custom errors."""

def __init__(self, message):
"""Constructor for a DockerExplorerError.
Args:
message (str): the error message.
"""
super(DockerExplorerError, self).__init__(message)
self.message = message


class BadContainerException(DockerExplorerError):
"""Raised when there was an issue parsing a Container configuration file."""


class BadStorageException(DockerExplorerError):
"""Raised when the Storage method detection failed."""
7 changes: 4 additions & 3 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import unittest

from docker_explorer import de
from docker_explorer import errors

from docker_explorer.lib import aufs
from docker_explorer.lib import overlay
Expand Down Expand Up @@ -110,7 +111,7 @@ def testDetectStorage(self):
'Please specify the Docker\'s directory path.\n'
'hint: de.py -r /var/lib/docker')

with self.assertRaises(de.BadStorageException) as err:
with self.assertRaises(errors.BadStorageException) as err:
de_test_object.DetectStorage()
self.assertEqual(expected_error_message, err.exception.message)

Expand Down Expand Up @@ -266,7 +267,7 @@ def testDetectStorage(self):
'Please specify the Docker\'s directory path.\n'
'hint: de.py -r /var/lib/docker')

with self.assertRaises(de.BadStorageException) as err:
with self.assertRaises(errors.BadStorageException) as err:
de_test_object.DetectStorage()
self.assertEqual(expected_error_message, err.exception.message)

Expand Down Expand Up @@ -421,7 +422,7 @@ def testDetectStorage(self):
'Please specify the Docker\'s directory path.\n'
'hint: de.py -r /var/lib/docker')

with self.assertRaises(de.BadStorageException) as err:
with self.assertRaises(errors.BadStorageException) as err:
de_test_object.DetectStorage()
self.assertEqual(expected_error_message, err.exception.message)

Expand Down

0 comments on commit 897201a

Please sign in to comment.