0
0
Nginxdevops~10 mins

JSON error responses in Nginx - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the content type to JSON in the error response.

Nginx
error_page 404 /404.json;
location = /404.json {
    default_type [1];
    return 404 '{"error": "Not Found"}';
}
Drag options to blanks, or click blank then click option'
Aapplication/json
Btext/html
Ctext/plain
Dapplication/xml
Attempts:
3 left
💡 Hint
Common Mistakes
Using text/html instead of application/json causes browsers to treat the response as a webpage.
Forgetting to set the content type leads to incorrect response handling.
2fill in blank
medium

Complete the code to return a 500 error with a JSON message.

Nginx
location /500.json {
    default_type application/json;
    return [1] '{"error": "Internal Server Error"}';
}
Drag options to blanks, or click blank then click option'
A400
B200
C404
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 means success, not an error.
Using 404 is for not found errors, not server errors.
3fill in blank
hard

Fix the error in the JSON error response to properly escape quotes.

Nginx
location /403.json {
    default_type application/json;
    return 403 '{"error": [1]Forbidden[2]';
}
Drag options to blanks, or click blank then click option'
A\"
B'
C"
D\'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unescaped double quotes breaks JSON syntax.
Using single quotes inside single quotes causes errors.
4fill in blank
hard

Fill both blanks to create a JSON error response with a custom message and status code.

Nginx
location /custom_error.json {
    default_type [1];
    return [2] '{"error": "Custom Error"}';
}
Drag options to blanks, or click blank then click option'
Aapplication/json
Btext/plain
C404
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Using text/plain causes browsers to treat the response as plain text.
Using 500 instead of 404 changes the error meaning.
5fill in blank
hard

Fill all three blanks to define a JSON error response with a dynamic error message and status code.

Nginx
location /error.json {
    default_type [1];
    set $err_msg '[2]';
    return [3] '{"error": "$err_msg"}';
}
Drag options to blanks, or click blank then click option'
Aapplication/json
BForbidden
C403
DInternal Server Error
Attempts:
3 left
💡 Hint
Common Mistakes
Using plain text content type breaks JSON formatting.
Mixing error messages and status codes incorrectly.