Skip to content

Commit cc288e2

Browse files
Merge pull request #7 from CodeSignal/fix/handle-400-json
Handling other 400 errors using JSON format
2 parents 3756c9c + 29d23ca commit cc288e2

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

app/routes/errors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ def handle_not_found_error(error):
99
"endpoint": request.path
1010
}), 404
1111

12+
@errors_bp.app_errorhandler(400)
13+
def handle_bad_request_error(error):
14+
return jsonify({
15+
"error": str(error),
16+
"endpoint": request.path
17+
}), 400
18+
1219
@errors_bp.app_errorhandler(500)
1320
def handle_internal_server_error(error):
1421
return jsonify({

test/auth_api_collection.json

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,60 @@
529529
}
530530
},
531531
{
532-
"name": "7d. Successful Logout with Both Tokens",
532+
"name": "7d. Try Logout with Invalid JSON (Should Return JSON Error)",
533+
"event": [
534+
{
535+
"listen": "test",
536+
"script": {
537+
"exec": [
538+
"pm.test('Status code is 400', function() {",
539+
" pm.response.to.have.status(400);",
540+
"});",
541+
"",
542+
"pm.test('Response is JSON', function() {",
543+
" pm.response.to.be.json;",
544+
"});",
545+
"",
546+
"pm.test('Error message is correct', function() {",
547+
" var jsonData = pm.response.json();",
548+
" pm.expect(jsonData.error).to.include('400 Bad Request');",
549+
" pm.expect(jsonData.error).to.include('The browser (or proxy) sent a request that this server could not understand');",
550+
" pm.expect(jsonData.endpoint).to.equal('/auth/logout');",
551+
"});"
552+
],
553+
"type": "text/javascript"
554+
}
555+
}
556+
],
557+
"request": {
558+
"method": "POST",
559+
"header": [
560+
{
561+
"key": "Content-Type",
562+
"value": "application/json",
563+
"type": "text"
564+
},
565+
{
566+
"key": "Authorization",
567+
"value": "Bearer {{jwt_token}}",
568+
"type": "text"
569+
}
570+
],
571+
"body": {
572+
"mode": "raw",
573+
"raw": "{ invalid json here"
574+
},
575+
"url": {
576+
"raw": "http://localhost:8000/auth/logout",
577+
"protocol": "http",
578+
"host": ["localhost"],
579+
"port": "8000",
580+
"path": ["auth", "logout"]
581+
}
582+
}
583+
},
584+
{
585+
"name": "7e. Successful Logout with Both Tokens",
533586
"event": [
534587
{
535588
"listen": "test",

0 commit comments

Comments
 (0)