Merge pull request #2715 from charltonaustin/fix_endpoint_error

fix(endpoints): added in proper error handling
This commit is contained in:
Charlton Austin 2017-06-21 17:00:38 -04:00 committed by GitHub
commit 0841d2bfb9
3 changed files with 27 additions and 4 deletions

View file

@ -62,7 +62,7 @@ def create_manifest_label(tag_manifest, key, value, source_type_name, media_type
media_type_id = _get_media_type_id(media_type_name) media_type_id = _get_media_type_id(media_type_name)
if media_type_id is None: if media_type_id is None:
raise InvalidMediaTypeException raise InvalidMediaTypeException()
source_type_id = _get_label_source_type_id(source_type_name) source_type_id = _get_label_source_type_id(source_type_name)

View file

@ -10,6 +10,7 @@ from endpoints.exception import NotFound
from data import model from data import model
from digest import digest_tools from digest import digest_tools
from util.validation import VALID_LABEL_KEY_REGEX
BASE_MANIFEST_ROUTE = '/v1/repository/<apirepopath:repository>/manifest/<regex("{0}"):manifestref>' BASE_MANIFEST_ROUTE = '/v1/repository/<apirepopath:repository>/manifest/<regex("{0}"):manifestref>'
MANIFEST_DIGEST_ROUTE = BASE_MANIFEST_ROUTE.format(digest_tools.DIGEST_PATTERN) MANIFEST_DIGEST_ROUTE = BASE_MANIFEST_ROUTE.format(digest_tools.DIGEST_PATTERN)
@ -92,9 +93,17 @@ class RepositoryManifestLabels(RepositoryParamResource):
if label_validator.has_reserved_prefix(label_data['key']): if label_validator.has_reserved_prefix(label_data['key']):
abort(400, message='Label has a reserved prefix') abort(400, message='Label has a reserved prefix')
label = model.label.create_manifest_label(tag_manifest, label_data['key'], label = None
label_data['value'], 'api', try:
media_type_name=label_data['media_type']) label = model.label.create_manifest_label(tag_manifest, label_data['key'],
label_data['value'], 'api',
media_type_name=label_data['media_type'])
except model.InvalidLabelKeyException:
abort(400, message='Label is of an invalid format or missing please use %s format for labels'.format(
VALID_LABEL_KEY_REGEX))
except model.InvalidMediaTypeException:
abort(400, message='Media type is invalid please use a valid media type of text/plain or application/json')
metadata = { metadata = {
'id': label.uuid, 'id': label.uuid,
'key': label_data['key'], 'key': label_data['key'],

View file

@ -4834,6 +4834,20 @@ class TestRepositoryManifestLabels(ApiTestCase):
self.assertEquals(0, len(json['labels'])) self.assertEquals(0, len(json['labels']))
self.postJsonResponse(RepositoryManifestLabels,
params=dict(repository=repository,
manifestref=tag_manifest.digest),
data=dict(key='bad_label', value='world',
media_type='text/plain'),
expected_code=400)
self.postJsonResponse(RepositoryManifestLabels,
params=dict(repository=repository,
manifestref=tag_manifest.digest),
data=dict(key='hello', value='world',
media_type='bad_media_type'),
expected_code=400)
# Add some labels to the manifest. # Add some labels to the manifest.
with assert_action_logged('manifest_label_add'): with assert_action_logged('manifest_label_add'):
label1 = self.postJsonResponse(RepositoryManifestLabels, label1 = self.postJsonResponse(RepositoryManifestLabels,