Skip to content

Commit

Permalink
Error checking when adding to blaze server.
Browse files Browse the repository at this point in the history
  • Loading branch information
kwmsmith committed Mar 23, 2016
1 parent 3ce9ff1 commit ea78b32
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 295 deletions.
63 changes: 41 additions & 22 deletions blaze/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ def _get_option(option, options, default=_no_default):
return default

# Provides a more informative error message.
raise TypeError(
'The blaze api must be registered with {option}'.format(
option=option,
),
)
msg = 'The blaze api must be registered with {option}'
raise TypeError(msg.format(option=option))


def ensure_dir(path):
Expand All @@ -70,12 +67,9 @@ def _register_api(app, options, first_registration=False):
Register the data with the blueprint.
"""
_get_data.cache[app] = _get_option('data', options)
_get_format.cache[app] = dict(
(f.name, f) for f in _get_option('formats', options)
)
_get_auth.cache[app] = (
_get_option('authorization', options, None) or (lambda a: True)
)
_get_format.cache[app] = dict((f.name, f) for f in _get_option('formats', options))
_get_auth.cache[app] = (_get_option('authorization', options, None) or
(lambda a: True))
allow_profiler = _get_option('allow_profiler', options, False)
profiler_output = _get_option('profiler_output', options, None)
profile_by_default = _get_option('profile_by_default', options, False)
Expand Down Expand Up @@ -607,21 +601,46 @@ def log_time(start=time()):
def addserver(payload, serial):
"""Add a data resource to the server.
The reuest should contain serialized MutableMapping (dictionary) like
object, and the server should already be hosting a MutableMapping
resource.
The request should contain serialized MutableMapping (dictionary) like
object, and the server should already be hosting a MutableMapping resource.
"""

data = _get_data.cache[flask.current_app]

data_not_mm_msg = ("Cannot update blaze server data since its current data"
" is a %s and not a mutable mapping (dictionary like).")
if not isinstance(data, collections.MutableMapping):
data_not_mm_msg = ("Cannot update blaze server data since its current "
"data is a %s and not a mutable mapping (dictionary "
"like).")
return (data_not_mm_msg % type(data), 422)

payload_not_mm_msg = ("Cannot update blaze server with a %s payload, since"
" it is not a mutable mapping (dictionary like).")
if not isinstance(payload, collections.MutableMapping):
return (payload_not_mm_msg % type(payload), 422)
if not isinstance(payload, collections.Mapping):
payload_not_mm_msg = ("Need a dictionary-like payload; instead was "
"given %s of type %s.")
return (payload_not_mm_msg % (payload, type(payload)), 422)

data.update(valmap(resource, payload))
return 'OK'
if len(payload) > 1:
error_msg = "Given more than one resource to add: %s"
return (error_msg % list(payload.keys()), 422)

[(name, resource_args)] = payload.items()

if name in data:
msg = "Cannot add dataset named %s, already exists on server."
return (msg % name, 409)

try:
data.update({name: resource(resource_args)})
# Force discovery of new dataset to check that the data is loadable.
ds = discover(data)
if name not in ds.dict:
raise ValueError("%s not added." % name)
except NotImplementedError as e:
error_msg = "Addition not supported:\n%s: %s"
return (error_msg % (type(e).__name__, e),
422)
except Exception as e:
error_msg = "Addition failed with message:\n%s: %s"
return (error_msg % (type(e).__name__, e),
422)

return ('OK', 200)
Loading

0 comments on commit ea78b32

Please sign in to comment.