0
0
Rest APIprogramming~20 mins

500 Internal Server Error in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
500 Internal Server Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this REST API error handler code?

Consider this Python Flask code snippet that handles errors:

from flask import Flask, jsonify
app = Flask(__name__)

@app.errorhandler(500)
def internal_error(error):
    return jsonify({'error': 'Internal Server Error'}), 500

@app.route('/cause_error')
def cause_error():
    1 / 0  # This will cause a ZeroDivisionError

if __name__ == '__main__':
    app.run()

What will the client receive when accessing /cause_error?

Rest API
from flask import Flask, jsonify
app = Flask(__name__)

@app.errorhandler(500)
def internal_error(error):
    return jsonify({'error': 'Internal Server Error'}), 500

@app.route('/cause_error')
def cause_error():
    1 / 0  # This will cause a ZeroDivisionError

if __name__ == '__main__':
    app.run()
A{"error": "Internal Server Error"} with HTTP status 500
BA JSON response with {'error': 'ZeroDivisionError'} and HTTP status 500
CA plain text message 'ZeroDivisionError' with HTTP status 200
DA server crash with no response sent to client
Attempts:
2 left
💡 Hint

Think about what the @app.errorhandler(500) decorator does.

🧠 Conceptual
intermediate
1:30remaining
Why does a 500 Internal Server Error occur in REST APIs?

Which of the following best explains the cause of a 500 Internal Server Error in a REST API?

AThe server encountered an unexpected condition that prevented it from fulfilling the request
BThe client is not authorized to access the requested resource
CThe requested resource was not found on the server
DThe client sent a malformed request that the server cannot understand
Attempts:
2 left
💡 Hint

Think about whether the problem is on the client or server side.

🔧 Debug
advanced
2:00remaining
Identify the cause of 500 Internal Server Error in this Node.js Express code

Look at this Node.js Express code snippet:

const express = require('express');
const app = express();

app.get('/data', (req, res) => {
  const data = JSON.parse('{invalid json}');
  res.json(data);
});

app.listen(3000);

What will happen when a client requests /data?

AThe server crashes and stops running
BThe server responds with an empty JSON object {} and status 200
CThe server responds with a 400 Bad Request error
DThe server responds with a 500 Internal Server Error due to JSON.parse failure
Attempts:
2 left
💡 Hint

Consider what happens when JSON.parse receives invalid JSON.

📝 Syntax
advanced
1:30remaining
Which code snippet correctly sends a 500 Internal Server Error in Express?

Choose the correct Express.js code to send a 500 Internal Server Error with a JSON message {"error": "Server failure"}:

Ares.json(500, {error: 'Server failure'});
Bres.status(500).json({error: 'Server failure'});
Cres.send(500, {error: 'Server failure'});
Dres.status(500).sendJson({error: 'Server failure'});
Attempts:
2 left
💡 Hint

Check the Express.js method chaining syntax for setting status and sending JSON.

🚀 Application
expert
2:30remaining
How to safely handle unexpected errors to avoid 500 Internal Server Errors in REST APIs?

You are designing a REST API. Which approach best prevents unhandled exceptions causing 500 Internal Server Errors?

AReturn HTTP 200 status with error details in the response body for all errors
BIgnore errors and let the server crash to restart cleanly
CWrap all code in try-catch blocks and return meaningful error responses for known errors
DUse client-side validation only and assume server code never fails
Attempts:
2 left
💡 Hint

Think about how to handle errors gracefully on the server side.