-
-
Notifications
You must be signed in to change notification settings - Fork 88
fix(import): return descriptive error messages and success response #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -370,18 +370,22 @@ class ImportAllResource(Resource): | |||||||||||||
| @api.expect(buckets_export) | ||||||||||||||
| @copy_doc(ServerAPI.import_all) | ||||||||||||||
| def post(self): | ||||||||||||||
| # If import comes from a form in th web-ui | ||||||||||||||
| if len(request.files) > 0: | ||||||||||||||
| # web-ui form only allows one file, but technically it's possible to | ||||||||||||||
| # upload multiple files at the same time | ||||||||||||||
| for filename, f in request.files.items(): | ||||||||||||||
| buckets = json.loads(f.stream.read())["buckets"] | ||||||||||||||
| try: | ||||||||||||||
| # If import comes from a form in the web-ui | ||||||||||||||
| if len(request.files) > 0: | ||||||||||||||
| # web-ui form only allows one file, but technically it's possible to | ||||||||||||||
| # upload multiple files at the same time | ||||||||||||||
| for filename, f in request.files.items(): | ||||||||||||||
| buckets = json.loads(f.stream.read())["buckets"] | ||||||||||||||
| current_app.api.import_all(buckets) | ||||||||||||||
| # Normal import from body | ||||||||||||||
| else: | ||||||||||||||
| buckets = request.get_json()["buckets"] | ||||||||||||||
| current_app.api.import_all(buckets) | ||||||||||||||
| # Normal import from body | ||||||||||||||
| else: | ||||||||||||||
| buckets = request.get_json()["buckets"] | ||||||||||||||
| current_app.api.import_all(buckets) | ||||||||||||||
| return None, 200 | ||||||||||||||
| except Exception as e: | ||||||||||||||
| logger.exception("Import failed") | ||||||||||||||
| return {"message": str(e)}, 400 | ||||||||||||||
|
Comment on lines
+385
to
+387
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||
| return {"message": "Import successful"}, 200 | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| # LOGGING | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exceptionmeans the caller cannot distinguish this user-facing validation error from unexpected runtime errors. A more specific type (e.g.,ValueError) letsrest.pyselectively catch expected errors and log them atWARNINGlevel rather thanERROR, while letting unexpected exceptions propagate differently if needed in the future.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!