Coverage for mindsdb / api / http / utils.py: 45%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1import json 

2from typing import Optional 

3from datetime import datetime 

4 

5from flask import Response 

6 

7 

8def http_error(status_code: int, title: Optional[str] = None, detail: Optional[str] = None): 

9 ''' Wrapper for error responce acoording with RFC 7807 (https://tools.ietf.org/html/rfc7807) 

10 

11 :param status_code: int - http status code for response 

12 :param title: str 

13 :param detail: str 

14 

15 :return: flask Response object 

16 ''' 

17 if title is None: 17 ↛ 18line 17 didn't jump to line 18 because the condition on line 17 was never true

18 title = 'Error' 

19 if detail is None: 19 ↛ 20line 19 didn't jump to line 20 because the condition on line 19 was never true

20 if 400 <= status_code < 500: 

21 detail = "A client error occurred. Please check your request and try again." 

22 elif 500 <= status_code < 600: 

23 detail = "A server error occurred. Please try again later." 

24 else: 

25 detail = "An error occurred while processing the request. Please try again later." 

26 

27 return Response( 

28 response=json.dumps({ 

29 'title': title, 

30 'detail': detail, 

31 'timestamp': str(datetime.now()) 

32 }), 

33 status=status_code, 

34 headers={ 

35 'Content-Type': 'application/problem+json' 

36 } 

37 )