Skip to content

Commit

Permalink
Fix first error status codes #79 #261
Browse files Browse the repository at this point in the history
  • Loading branch information
csenger committed Mar 21, 2017
1 parent 8d9a2c3 commit 29dcc7e
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/plone/restapi/services/content/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
}


class ErrorMixin(object):

def _error(self, type, message, status=400):
'''
Set a status code (400 is the default error in the TUS
reference server implementation) and return a plone.restapi
conform error body.
'''
self.request.response.setStatus(status)
return {'error': {
'type': type,
'message': message,
}}


class UploadOptions(Service):
"""TUS upload endpoint for handling OPTIONS requests without CORS."""

Expand All @@ -32,7 +47,7 @@ def reply(self):
return super(UploadOptions, self).reply()


class UploadPost(Service):
class UploadPost(Service, ErrorMixin):
"""TUS upload endpoint for handling POST requests."""

def __call__(self):
Expand All @@ -55,11 +70,8 @@ def reply(self):
try:
length = int(length)
except ValueError:
return {'error': {
'type': 'Bad Request',
'message': 'Missing or invalid Upload-Length header'
}}

return self._error('Bad Request',
'Missing or invalid Upload-Length header')
# Parse metadata
metadata = {}
for item in self.request.getHeader('Upload-Metadata', '').split(','):
Expand All @@ -76,7 +88,7 @@ def reply(self):
self.context.absolute_url(), tus_upload.uid))


class UploadFileBase(Service):
class UploadFileBase(Service, ErrorMixin):
implements(IPublishTraverse)

def __init__(self, context, request):
Expand Down Expand Up @@ -126,10 +138,7 @@ class UploadPatch(UploadFileBase):

def reply(self):
if self.uid is None:
return {'error': {
'type': 'Bad Request',
'message': 'Missing UID'
}}
return self._error('Bad Request', 'Missing UID')

if not self.check_tus_version():
return {'error': {
Expand Down

0 comments on commit 29dcc7e

Please sign in to comment.