1# -*- coding: utf-8 -*- 2# Copyright 2010 Google Inc. All Rights Reserved. 3# 4# Permission is hereby granted, free of charge, to any person obtaining a 5# copy of this software and associated documentation files (the 6# "Software"), to deal in the Software without restriction, including 7# without limitation the rights to use, copy, modify, merge, publish, dis- 8# tribute, sublicense, and/or sell copies of the Software, and to permit 9# persons to whom the Software is furnished to do so, subject to the fol- 10# lowing conditions: 11# 12# The above copyright notice and this permission notice shall be included 13# in all copies or substantial portions of the Software. 14# 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- 17# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 18# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 19# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21# IN THE SOFTWARE. 22"""gsutil exceptions. 23 24The exceptions in this module are for use across multiple different classes. 25""" 26 27from __future__ import absolute_import 28from __future__ import print_function 29from __future__ import division 30from __future__ import unicode_literals 31 32import six 33 34NO_URLS_MATCHED_GENERIC = 'No URLs matched' 35NO_URLS_MATCHED_TARGET = 'No URLs matched: %s' 36 37if six.PY3: 38 # StandardError was removed, so use the base exception type instead 39 StandardError = Exception 40 41 42class AbortException(StandardError): 43 """Exception raised when a user aborts a command that needs to do cleanup.""" 44 45 def __init__(self, reason): 46 StandardError.__init__(self) 47 self.reason = reason 48 49 def __repr__(self): 50 return 'AbortException: %s' % self.reason 51 52 def __str__(self): 53 return 'AbortException: %s' % self.reason 54 55 56class CommandException(StandardError): 57 """Exception raised when a problem is encountered running a gsutil command. 58 59 This exception should be used to signal user errors or system failures 60 (like timeouts), not bugs (like an incorrect param value). For the 61 latter you should raise Exception so we can see where/how it happened 62 via gsutil -D (which will include a stack trace for raised Exceptions). 63 """ 64 65 def __init__(self, reason, informational=False): 66 """Instantiate a CommandException. 67 68 Args: 69 reason: Text describing the problem. 70 informational: Indicates reason should be printed as FYI, not a failure. 71 """ 72 StandardError.__init__(self) 73 self.reason = reason 74 self.informational = informational 75 76 def __repr__(self): 77 return str(self) 78 79 def __str__(self): 80 return 'CommandException: %s' % self.reason 81 82 83class ControlCException(Exception): 84 """Exception to report to analytics when the user exits via ctrl-C. 85 86 This exception is never actually raised, but is used by analytics collection 87 to provide a more descriptive name for user exit. 88 """ 89 pass 90 91 92class HashMismatchException(Exception): 93 """Exception raised when data integrity validation fails.""" 94 pass 95 96 97class IamChOnResourceWithConditionsException(Exception): 98 """Raised when trying to use "iam ch" on an IAM policy with conditions. 99 100 Because the syntax for conditions is fairly complex, it doesn't make sense to 101 specify them on the command line using a colon-delimited set of values in the 102 same way you'd specify simple bindings - it would be a complex and potentially 103 surprising interface, which isn't what you want when dealing with permissions. 104 105 Additionally, providing partial functionality -- e.g. if a policy contains 106 bindings with conditions, still allow users to interact with bindings that 107 don't contain conditions -- might sound tempting, but results in a bad user 108 experience. Bindings can be thought of as a mapping from (role, condition) -> 109 [members]. Thus, a user might think they're editing the binding for (role1, 110 condition1), but they'd really be editing the binding for (role1, None). Thus, 111 we just raise an error if we encounter a binding with conditions present, and 112 encourage users to use "iam {get,set}" instead. 113 """ 114 115 def __init__(self, message): 116 Exception.__init__(self, message) 117 self.message = message 118 119 def __repr__(self): 120 return str(self) 121 122 def __str__(self): 123 return 'IamChOnResourceWithConditionsException: %s' % self.message 124 125 126class InvalidUrlError(Exception): 127 """Exception raised when URL is invalid.""" 128 129 def __init__(self, message): 130 Exception.__init__(self, message) 131 self.message = message 132 133 def __repr__(self): 134 return str(self) 135 136 def __str__(self): 137 return 'InvalidUrlError: %s' % self.message 138