Coverage for core/routes_error.py: 49%

303 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-05 14:02 +0000

1"""This module contains the error routes for the application.""" 

2 

3from flask import json, redirect, render_template, session 

4 

5from werkzeug import exceptions 

6 

7 

8def get_user_type(): 

9 """Get the user type from the session data.""" 

10 user = session.get("user") 

11 employer = session.get("employer") 

12 student = session.get("student") 

13 superuser = session.get("superuser") 

14 

15 user_type = None 

16 # Determine user_type based on session data 

17 if superuser: 

18 user_type = "superuser" 

19 elif user: 

20 user_type = "admin" 

21 elif employer: 

22 user_type = "employer" 

23 elif student: 

24 user_type = "student" 

25 return user_type 

26 

27 

28def add_error_routes(app): 

29 """Register error routes for the application.""" 

30 

31 @app.route("/400") 

32 def error_400(): 

33 """The 400 route which renders the '400.html' template. 

34 

35 Returns: 

36 str: Rendered 400.html template. 

37 """ 

38 code = 400 

39 name = "Bad Request" 

40 msg = "The server could not understand the request due to invalid syntax." 

41 return ( 

42 render_template( 

43 "error_page.html", 

44 user_type=get_user_type(), 

45 code=code, 

46 name=name, 

47 msg=msg, 

48 ), 

49 400, 

50 ) 

51 

52 @app.route("/401") 

53 def error_401(): 

54 """The 401 route which renders the '401.html' template. 

55 

56 Returns: 

57 str: Rendered 401.html template. 

58 """ 

59 code = 401 

60 name = "Unauthorized" 

61 msg = "The server could not verify that you are authorized to access the URL requested." 

62 return ( 

63 render_template( 

64 "error_page.html", 

65 user_type=get_user_type(), 

66 code=code, 

67 name=name, 

68 msg=msg, 

69 ), 

70 401, 

71 ) 

72 

73 @app.route("/403") 

74 def error_403(): 

75 """The 403 route which renders the '403.html' template. 

76 

77 Returns: 

78 str: Rendered 403.html template. 

79 """ 

80 code = 403 

81 name = "Forbidden" 

82 msg = "You don't have permission to access the requested resource." 

83 return ( 

84 render_template( 

85 "error_page.html", 

86 user_type=get_user_type(), 

87 code=code, 

88 name=name, 

89 msg=msg, 

90 ), 

91 403, 

92 ) 

93 

94 @app.route("/404") 

95 def error_404(): 

96 """The 404 route which renders the '404.html' template. 

97 

98 Returns: 

99 str: Rendered 404.html template. 

100 """ 

101 code = 404 

102 name = "Not Found" 

103 msg = "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again." 

104 return ( 

105 render_template( 

106 "error_page.html", 

107 user_type=get_user_type(), 

108 code=code, 

109 name=name, 

110 msg=msg, 

111 ), 

112 404, 

113 ) 

114 

115 @app.route("/405") 

116 def error_405(): 

117 """The 405 route which renders the '405.html' template. 

118 

119 Returns: 

120 str: Rendered 405.html template. 

121 """ 

122 code = 405 

123 name = "Method Not Allowed" 

124 msg = "The method is not allowed for the requested URL." 

125 return ( 

126 render_template( 

127 "error_page.html", 

128 user_type=get_user_type(), 

129 code=code, 

130 name=name, 

131 msg=msg, 

132 ), 

133 405, 

134 ) 

135 

136 @app.route("/406") 

137 def error_406(): 

138 """The 406 route which renders the '406.html' template. 

139 

140 Returns: 

141 str: Rendered 406.html template. 

142 """ 

143 code = 406 

144 name = "Not Acceptable" 

145 msg = "The server can only generate a response that is not accepted by the client." 

146 return ( 

147 render_template( 

148 "error_page.html", 

149 user_type=get_user_type(), 

150 code=code, 

151 name=name, 

152 msg=msg, 

153 ), 

154 406, 

155 ) 

156 

157 @app.route("/408") 

158 def error_408(): 

159 """The 408 route which renders the '408.html' template. 

160 

161 Returns: 

162 str: Rendered 408.html template. 

163 """ 

164 code = 408 

165 name = "Request Timeout" 

166 msg = "The server timed out waiting for the request." 

167 return ( 

168 render_template( 

169 "error_page.html", 

170 user_type=get_user_type(), 

171 code=code, 

172 name=name, 

173 msg=msg, 

174 ), 

175 408, 

176 ) 

177 

178 @app.route("/409") 

179 def error_409(): 

180 code = 409 

181 name = "Conflict" 

182 msg = "The request could not be completed due to a conflict with the current state of the resource." 

183 return ( 

184 render_template( 

185 "error_page.html", 

186 user_type=get_user_type(), 

187 code=code, 

188 name=name, 

189 msg=msg, 

190 ), 

191 409, 

192 ) 

193 

194 @app.route("/410") 

195 def error_410(): 

196 code = 410 

197 name = "Gone" 

198 msg = "The requested resource is no longer available and has been permanently removed." 

199 return ( 

200 render_template( 

201 "error_page.html", 

202 user_type=get_user_type(), 

203 code=code, 

204 name=name, 

205 msg=msg, 

206 ), 

207 410, 

208 ) 

209 

210 @app.route("/411") 

211 def error_411(): 

212 code = 411 

213 name = "Length Required" 

214 msg = ( 

215 "The server refuses to accept the request without a defined Content-Length." 

216 ) 

217 return ( 

218 render_template( 

219 "error_page.html", 

220 user_type=get_user_type(), 

221 code=code, 

222 name=name, 

223 msg=msg, 

224 ), 

225 411, 

226 ) 

227 

228 @app.route("/412") 

229 def error_412(): 

230 code = 412 

231 name = "Precondition Failed" 

232 msg = "The server does not meet one of the preconditions specified in the request." 

233 return ( 

234 render_template( 

235 "error_page.html", 

236 user_type=get_user_type(), 

237 code=code, 

238 name=name, 

239 msg=msg, 

240 ), 

241 412, 

242 ) 

243 

244 @app.route("/413") 

245 def error_413(): 

246 code = 413 

247 name = "Request Entity Too Large" 

248 msg = "The request is larger than the server is willing or able to process." 

249 return ( 

250 render_template( 

251 "error_page.html", 

252 user_type=get_user_type(), 

253 code=code, 

254 name=name, 

255 msg=msg, 

256 ), 

257 413, 

258 ) 

259 

260 @app.route("/414") 

261 def error_414(): 

262 code = 414 

263 name = "Request URI Too Large" 

264 msg = "The URI requested by the client is longer than the server is willing to interpret." 

265 return ( 

266 render_template( 

267 "error_page.html", 

268 user_type=get_user_type(), 

269 code=code, 

270 name=name, 

271 msg=msg, 

272 ), 

273 414, 

274 ) 

275 

276 @app.route("/415") 

277 def error_415(): 

278 code = 415 

279 name = "Unsupported Media Type" 

280 msg = "The server does not support the media type transmitted in the request." 

281 return ( 

282 render_template( 

283 "error_page.html", 

284 user_type=get_user_type(), 

285 code=code, 

286 name=name, 

287 msg=msg, 

288 ), 

289 415, 

290 ) 

291 

292 @app.route("/416") 

293 def error_416(): 

294 code = 416 

295 name = "Requested Range Not Satisfiable" 

296 msg = "The client has asked for a portion of the file that the server cannot supply." 

297 return ( 

298 render_template( 

299 "error_page.html", 

300 user_type=get_user_type(), 

301 code=code, 

302 name=name, 

303 msg=msg, 

304 ), 

305 416, 

306 ) 

307 

308 @app.route("/417") 

309 def error_417(): 

310 code = 417 

311 name = "Expectation Failed" 

312 msg = "The server cannot meet the requirements of the Expect request-header field." 

313 return ( 

314 render_template( 

315 "error_page.html", 

316 user_type=get_user_type(), 

317 code=code, 

318 name=name, 

319 msg=msg, 

320 ), 

321 417, 

322 ) 

323 

324 @app.route("/418") 

325 def error_418(): 

326 code = 418 

327 name = "I'm a teapot" 

328 msg = "The server is a teapot; it cannot brew coffee." 

329 return ( 

330 render_template( 

331 "error_page.html", 

332 user_type=get_user_type(), 

333 code=code, 

334 name=name, 

335 msg=msg, 

336 ), 

337 418, 

338 ) 

339 

340 @app.route("/421") 

341 def error_421(): 

342 code = 421 

343 name = "Misdirected Request" 

344 msg = "The request was directed at a server that is not able to produce a response." 

345 return ( 

346 render_template( 

347 "error_page.html", 

348 user_type=get_user_type(), 

349 code=code, 

350 name=name, 

351 msg=msg, 

352 ), 

353 421, 

354 ) 

355 

356 @app.route("/422") 

357 def error_422(): 

358 code = 422 

359 name = "Unprocessable Entity" 

360 msg = "The server understands the content type of the request entity, but was unable to process the contained instructions." 

361 return ( 

362 render_template( 

363 "error_page.html", 

364 user_type=get_user_type(), 

365 code=code, 

366 name=name, 

367 msg=msg, 

368 ), 

369 422, 

370 ) 

371 

372 @app.route("/423") 

373 def error_423(): 

374 code = 423 

375 name = "Locked" 

376 msg = "The resource that is being accessed is locked." 

377 return ( 

378 render_template( 

379 "error_page.html", 

380 user_type=get_user_type(), 

381 code=code, 

382 name=name, 

383 msg=msg, 

384 ), 

385 423, 

386 ) 

387 

388 @app.route("/424") 

389 def error_424(): 

390 code = 424 

391 name = "Failed Dependency" 

392 msg = "The method could not be performed on the resource because the requested action depended on another action and that action failed." 

393 return ( 

394 render_template( 

395 "error_page.html", 

396 user_type=get_user_type(), 

397 code=code, 

398 name=name, 

399 msg=msg, 

400 ), 

401 424, 

402 ) 

403 

404 @app.route("/428") 

405 def error_428(): 

406 code = 428 

407 name = "Precondition Required" 

408 msg = "The server requires the request to be conditional." 

409 return ( 

410 render_template( 

411 "error_page.html", 

412 user_type=get_user_type(), 

413 code=code, 

414 name=name, 

415 msg=msg, 

416 ), 

417 428, 

418 ) 

419 

420 @app.route("/429") 

421 def error_429(): 

422 code = 429 

423 name = "Too Many Requests" 

424 msg = "The user has sent too many requests in a given amount of time." 

425 return ( 

426 render_template( 

427 "error_page.html", 

428 user_type=get_user_type(), 

429 code=code, 

430 name=name, 

431 msg=msg, 

432 ), 

433 429, 

434 ) 

435 

436 @app.route("/431") 

437 def error_431(): 

438 code = 431 

439 name = "Request Header Fields Too Large" 

440 msg = "The server is unwilling to process the request because its header fields are too large." 

441 return ( 

442 render_template( 

443 "error_page.html", 

444 user_type=get_user_type(), 

445 code=code, 

446 name=name, 

447 msg=msg, 

448 ), 

449 431, 

450 ) 

451 

452 @app.route("/451") 

453 def error_451(): 

454 code = 451 

455 name = "Unavailable For Legal Reasons" 

456 msg = "The server is denying access to the resource as a consequence of a legal demand." 

457 return ( 

458 render_template( 

459 "error_page.html", 

460 user_type=get_user_type(), 

461 code=code, 

462 name=name, 

463 msg=msg, 

464 ), 

465 451, 

466 ) 

467 

468 @app.route("/500") 

469 def error_500(): 

470 """The 500 route which renders the '500.html' template. 

471 

472 Returns: 

473 str: Rendered 500.html template. 

474 """ 

475 code = 500 

476 name = "Internal Server Error" 

477 msg = "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application." 

478 return ( 

479 render_template( 

480 "error_page.html", 

481 user_type=get_user_type(), 

482 code=code, 

483 name=name, 

484 msg=msg, 

485 ), 

486 500, 

487 ) 

488 

489 @app.route("/501") 

490 def error_501(): 

491 code = 501 

492 name = "Not Implemented" 

493 msg = "The server either does not recognize the request method, or it lacks the ability to fulfill the request." 

494 return ( 

495 render_template( 

496 "error_page.html", 

497 user_type=get_user_type(), 

498 code=code, 

499 name=name, 

500 msg=msg, 

501 ), 

502 501, 

503 ) 

504 

505 @app.route("/502") 

506 def error_502(): 

507 code = 502 

508 name = "Bad Gateway" 

509 msg = "The server was acting as a gateway or proxy and received an invalid response from the upstream server." 

510 return ( 

511 render_template( 

512 "error_page.html", 

513 user_type=get_user_type(), 

514 code=code, 

515 name=name, 

516 msg=msg, 

517 ), 

518 502, 

519 ) 

520 

521 @app.route("/503") 

522 def error_503(): 

523 code = 503 

524 name = "Service Unavailable" 

525 msg = "The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state." 

526 return ( 

527 render_template( 

528 "error_page.html", 

529 user_type=get_user_type(), 

530 code=code, 

531 name=name, 

532 msg=msg, 

533 ), 

534 503, 

535 ) 

536 

537 @app.route("/504") 

538 def error_504(): 

539 code = 504 

540 name = "Gateway Timeout" 

541 msg = "The server was acting as a gateway or proxy and did not receive a timely response from the upstream server." 

542 return ( 

543 render_template( 

544 "error_page.html", 

545 user_type=get_user_type(), 

546 code=code, 

547 name=name, 

548 msg=msg, 

549 ), 

550 504, 

551 ) 

552 

553 @app.route("/505") 

554 def error_505(): 

555 code = 505 

556 name = "HTTP Version Not Supported" 

557 msg = ( 

558 "The server does not support the HTTP protocol version used in the request." 

559 ) 

560 return ( 

561 render_template( 

562 "error_page.html", 

563 user_type=get_user_type(), 

564 code=code, 

565 name=name, 

566 msg=msg, 

567 ), 

568 505, 

569 ) 

570 

571 @app.errorhandler(exceptions.BadRequest) 

572 def handle_bad_request(_e): 

573 """Handle bad requests and render the 400 error page.""" 

574 return redirect("/400") 

575 

576 @app.errorhandler(exceptions.Unauthorized) 

577 def handle_unauthorized(_e): 

578 """Handle unauthorized errors and render the 401 error page.""" 

579 return redirect("/401") 

580 

581 @app.errorhandler(exceptions.Forbidden) 

582 def handle_forbidden(_e): 

583 """Handle forbidden errors and render the 403 error page.""" 

584 return redirect("/403") 

585 

586 @app.errorhandler(exceptions.NotFound) 

587 def handle_not_found(_e): 

588 """Handle not found errors and render the 404 error page.""" 

589 return redirect("/404") 

590 

591 @app.errorhandler(exceptions.MethodNotAllowed) 

592 def handle_method_not_allowed(_e): 

593 """Handle method not allowed errors and render the 405 error page.""" 

594 return redirect("/405") 

595 

596 @app.errorhandler(exceptions.NotAcceptable) 

597 def handle_not_acceptable(_e): 

598 """Handle not acceptable errors and render the 406 error page.""" 

599 return redirect("/406") 

600 

601 @app.errorhandler(exceptions.RequestTimeout) 

602 def handle_request_timeout(_e): 

603 """Handle request timeout errors and render the 408 error page.""" 

604 return redirect("/408") 

605 

606 @app.errorhandler(exceptions.Conflict) 

607 def handle_conflict(_e): 

608 """Handle conflict errors and render the 409 error page.""" 

609 return redirect("/409") 

610 

611 @app.errorhandler(exceptions.Gone) 

612 def handle_gone(_e): 

613 """Handle gone errors and render the 410 error page.""" 

614 return redirect("/410") 

615 

616 @app.errorhandler(exceptions.LengthRequired) 

617 def handle_length_required(_e): 

618 """Handle length required errors and render the 411 error page.""" 

619 return redirect("/411") 

620 

621 @app.errorhandler(exceptions.PreconditionFailed) 

622 def handle_precondition_failed(_e): 

623 """Handle precondition failed errors and render the 412 error page.""" 

624 return redirect("/412") 

625 

626 @app.errorhandler(exceptions.RequestEntityTooLarge) 

627 def handle_request_entity_too_large(_e): 

628 """Handle request entity too large errors and render the 413 error page.""" 

629 return redirect("/413") 

630 

631 @app.errorhandler(exceptions.RequestURITooLarge) 

632 def handle_request_uri_too_large(_e): 

633 """Handle request URI too large errors and render the 414 error page.""" 

634 return redirect("/414") 

635 

636 @app.errorhandler(exceptions.UnsupportedMediaType) 

637 def handle_unsupported_media_type(_e): 

638 """Handle unsupported media type errors and render the 415 error page.""" 

639 return redirect("/415") 

640 

641 @app.errorhandler(exceptions.RequestedRangeNotSatisfiable) 

642 def handle_requested_range_not_satisfiable(_e): 

643 """Handle requested range not satisfiable errors and render the 416 error page.""" 

644 return redirect("/416") 

645 

646 @app.errorhandler(exceptions.ExpectationFailed) 

647 def handle_expectation_failed(_e): 

648 """Handle expectation failed errors and render the 417 error page.""" 

649 return redirect("/417") 

650 

651 @app.errorhandler(exceptions.ImATeapot) 

652 def handle_im_a_teapot(_e): 

653 """Handle I'm a teapot errors and render the 418 error page.""" 

654 return redirect("/418") 

655 

656 @app.errorhandler(exceptions.MisdirectedRequest) 

657 def handle_misdirected_request(_e): 

658 """Handle misdirected request errors and render the 421 error page.""" 

659 return redirect("/421") 

660 

661 @app.errorhandler(exceptions.UnprocessableEntity) 

662 def handle_unprocessable_entity(_e): 

663 """Handle unprocessable entity errors and render the 422 error page.""" 

664 return redirect("/422") 

665 

666 @app.errorhandler(exceptions.Locked) 

667 def handle_locked(_e): 

668 """Handle locked errors and render the 423 error page.""" 

669 return redirect("/423") 

670 

671 @app.errorhandler(exceptions.FailedDependency) 

672 def handle_failed_dependency(_e): 

673 """Handle failed dependency errors and render the 424 error page.""" 

674 return redirect("/424") 

675 

676 @app.errorhandler(exceptions.PreconditionRequired) 

677 def handle_precondition_required(_e): 

678 """Handle precondition required errors and render the 428 error page.""" 

679 return redirect("/428") 

680 

681 @app.errorhandler(exceptions.TooManyRequests) 

682 def handle_too_many_requests(_e): 

683 """Handle too many requests errors and render the 429 error page.""" 

684 return redirect("/429") 

685 

686 @app.errorhandler(exceptions.RequestHeaderFieldsTooLarge) 

687 def handle_request_header_fields_too_large(_e): 

688 """Handle request header fields too large errors and render the 431 error page.""" 

689 return redirect("/431") 

690 

691 @app.errorhandler(exceptions.UnavailableForLegalReasons) 

692 def handle_unavailable_for_legal_reasons(_e): 

693 """Handle unavailable for legal reasons errors and render the 451 error page.""" 

694 return redirect("/451") 

695 

696 @app.errorhandler(exceptions.InternalServerError) 

697 def handle_internal_server_error(_e): 

698 """Handle internal server errors and render the 500 error page.""" 

699 return redirect("/500") 

700 

701 @app.errorhandler(exceptions.NotImplemented) 

702 def handle_not_implemented(_e): 

703 """Handle not implemented errors and render the 501 error page.""" 

704 return redirect("/501") 

705 

706 @app.errorhandler(exceptions.BadGateway) 

707 def handle_bad_gateway(_e): 

708 """Handle bad gateway errors and render the 502 error page.""" 

709 return redirect("/502") 

710 

711 @app.errorhandler(exceptions.ServiceUnavailable) 

712 def handle_service_unavailable(_e): 

713 """Handle service unavailable errors and render the 503 error page.""" 

714 return redirect("/503") 

715 

716 @app.errorhandler(exceptions.GatewayTimeout) 

717 def handle_gateway_timeout(_e): 

718 """Handle gateway timeout errors and render the 504 error page.""" 

719 return redirect("/504") 

720 

721 @app.errorhandler(exceptions.HTTPVersionNotSupported) 

722 def handle_http_version_not_supported(_e): 

723 """Handle HTTP version not supported errors and render the 505 error page.""" 

724 return redirect("/505") 

725 

726 @app.errorhandler(exceptions.HTTPException) 

727 def handle_exception(e): 

728 """Return JSON instead of HTML for HTTP errors.""" 

729 response = e.get_response() 

730 response.data = json.dumps( 

731 { 

732 "code": e.code, 

733 "name": e.name, 

734 "description": e.description, 

735 } 

736 ) 

737 response.content_type = "application/json" 

738 return response