fix: [api] fix errors handler 404 405, return json

This commit is contained in:
Terrtia 2019-07-31 13:24:43 +02:00
parent 79ad809d2c
commit 918b4c28ed
No known key found for this signature in database
GPG key ID: 1E1B1F50D84613D0
3 changed files with 18 additions and 3 deletions

View file

@ -49,7 +49,7 @@ def check_import_status(UUID):
processing = r_serv_log_submit.get(UUID + ':processing')
if not processing:
return ({'status': 'error', 'reason': 'Unknow uuid'}, 400)
return ({'status': 'error', 'reason': 'Unknown uuid'}, 400)
# nb_total = r_serv_log_submit.get(UUID + ':nb_total')
# nb_sucess = r_serv_log_submit.get(UUID + ':nb_sucess')

View file

@ -5,6 +5,7 @@ import os
import re
import sys
import ssl
import json
import time
import redis
@ -13,7 +14,7 @@ import logging
import logging.handlers
import configparser
from flask import Flask, render_template, jsonify, request, Request, session, redirect, url_for
from flask import Flask, render_template, jsonify, request, Request, Response, session, redirect, url_for
from flask_login import LoginManager, current_user, login_user, logout_user, login_required
import bcrypt
@ -291,7 +292,21 @@ def searchbox():
# ========== ERROR HANDLER ============
@app.errorhandler(405)
def _handle_client_error(e):
if request.path.startswith('/api/'):
return Response(json.dumps({"status": "error", "reason": "Method Not Allowed: The method is not allowed for the requested URL"}, indent=2, sort_keys=True), mimetype='application/json'), 405
else:
return e
@app.errorhandler(404)
def error_page_not_found(e):
if request.path.startswith('/api/'):
return Response(json.dumps({"status": "error", "reason": "404 Not Found"}, indent=2, sort_keys=True), mimetype='application/json'), 404
else:
# avoid endpoint enumeration
return page_not_found(e)
@login_required
def page_not_found(e):
# avoid endpoint enumeration

View file

@ -627,7 +627,7 @@ def import_item_uuid(UUID):
HTTP Status Code: 400
{'status': 'error', 'reason': 'Invalid uuid'}
{'status': 'error', 'reason': 'Unknow uuid'}
{'status': 'error', 'reason': 'Unknown uuid'}
"""