Make LayerTooLarge error more informative

This commit is contained in:
Joseph Schorr 2017-03-14 13:41:16 -04:00
parent dd7f254f96
commit 239b6d7cf8
2 changed files with 20 additions and 14 deletions

View file

@ -1,3 +1,5 @@
import bitmath
class V2RegistryException(Exception):
def __init__(self, error_code_str, message, detail, http_status_code=400,
repository=None, scopes=None):
@ -113,9 +115,23 @@ class TagInvalid(V2RegistryException):
detail)
class LayerTooLarge(V2RegistryException):
def __init__(self, detail=None):
def __init__(self, uploaded=None, max_allowed=None):
detail = {}
message = 'Uploaded blob is larger than allowed by this registry'
if uploaded is not None and max_allowed is not None:
detail = {
'reason': '%s is greater than maximum allowed size %s' % (uploaded, max_allowed),
'max_allowed': max_allowed,
'uploaded': uploaded,
}
up_str = bitmath.Byte(uploaded).best_prefix().format("{value:.2f} {unit}")
max_str = bitmath.Byte(max_allowed).best_prefix().format("{value:.2f} {unit}")
message = 'Uploaded blob of %s is larger than %s allowed by this registry' % (up_str, max_str)
super(LayerTooLarge, self).__init__('BLOB_UPLOAD_INVALID',
'Uploaded layer is larger than allowed by this registry',
message,
detail)