Coverage for /Users/davegaeddert/Development/dropseed/plain/plain/plain/views/errors.py: 48%

21 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-16 22:04 -0500

1from plain.http import ResponseBase 

2from plain.templates import TemplateFileMissing 

3 

4from .templates import TemplateView 

5 

6 

7class ErrorView(TemplateView): 

8 status_code: int 

9 

10 def __init__(self, status_code=None) -> None: 

11 # Allow creating an ErrorView with a status code 

12 # e.g. ErrorView.as_view(status_code=404) 

13 if status_code is not None: 

14 self.status_code = status_code 

15 

16 def get_template_names(self) -> list[str]: 

17 return [f"{self.status_code}.html", "error.html"] 

18 

19 def get_request_handler(self): 

20 return self.get # All methods (post, patch, etc.) will use the get() 

21 

22 def get_response(self) -> ResponseBase: 

23 response = super().get_response() 

24 # Set the status code we want 

25 response.status_code = self.status_code 

26 return response 

27 

28 def get(self): 

29 try: 

30 return super().get() 

31 except TemplateFileMissing: 

32 return self.status_code