aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/exception.py
blob: 0433dc9728d6909117c056aae44415e197c2647c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-

# Copyright 2015-2021 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

"""Exception classes used by gallery-dl

Class Hierarchy:

Exception
 +-- GalleryDLException
      +-- ExtractionError
      |    +-- AuthenticationError
      |    +-- AuthorizationError
      |    +-- NotFoundError
      |    +-- HttpError
      +-- FormatError
      |    +-- FilenameFormatError
      |    +-- DirectoryFormatError
      +-- FilterError
      +-- NoExtractorError
      +-- StopExtraction
      +-- TerminateExtraction
"""


class GalleryDLException(Exception):
    """Base class for GalleryDL exceptions"""
    default = None
    msgfmt = None
    code = 1

    def __init__(self, message=None):
        if not message:
            message = self.default
        elif isinstance(message, Exception):
            message = "{}: {}".format(message.__class__.__name__, message)
        if self.msgfmt:
            message = self.msgfmt.format(message)
        Exception.__init__(self, message)


class ExtractionError(GalleryDLException):
    """Base class for exceptions during information extraction"""


class HttpError(ExtractionError):
    """HTTP request during data extraction failed"""
    default = "HTTP request failed"
    code = 4

    def __init__(self, message, response=None):
        ExtractionError.__init__(self, message)
        self.response = response
        self.status = response.status_code if response else 0


class NotFoundError(ExtractionError):
    """Requested resource (gallery/image) could not be found"""
    msgfmt = "Requested {} could not be found"
    default = "resource (gallery/image)"
    code = 8


class AuthenticationError(ExtractionError):
    """Invalid or missing login credentials"""
    default = "Invalid or missing login credentials"
    code = 16


class AuthorizationError(ExtractionError):
    """Insufficient privileges to access a resource"""
    default = "Insufficient privileges to access the specified resource"
    code = 16


class FormatError(GalleryDLException):
    """Error while building output paths"""
    code = 32


class FilenameFormatError(FormatError):
    """Error while building output filenames"""
    msgfmt = "Applying filename format string failed ({})"


class DirectoryFormatError(FormatError):
    """Error while building output directory paths"""
    msgfmt = "Applying directory format string failed ({})"


class FilterError(GalleryDLException):
    """Error while evaluating a filter expression"""
    msgfmt = "Evaluating filter expression failed ({})"
    code = 32


class NoExtractorError(GalleryDLException):
    """No extractor can handle the given URL"""
    code = 64


class StopExtraction(GalleryDLException):
    """Stop data extraction"""

    def __init__(self, message=None, *args):
        GalleryDLException.__init__(self)
        self.message = message % args if args else message
        self.code = 1 if message else 0


class TerminateExtraction(GalleryDLException):
    """Terminate data extraction"""
    code = 0