mirror of
				https://github.com/Telecominfraproject/oopt-gnpy.git
				synced 2025-11-04 03:57:47 +00:00 
			
		
		
		
	Put api in a dedicated python package
Signed-off-by: manuedelf <59697943+edelfour@users.noreply.github.com>
This commit is contained in:
		
				
					committed by
					
						
						EstherLerouzic
					
				
			
			
				
	
			
			
			
						parent
						
							84fd574df9
						
					
				
				
					commit
					396020eeb5
				
			
							
								
								
									
										31
									
								
								gnpy/api/exception/exception_handler.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								gnpy/api/exception/exception_handler.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
# coding: utf-8
 | 
			
		||||
import json
 | 
			
		||||
import re
 | 
			
		||||
 | 
			
		||||
import werkzeug
 | 
			
		||||
 | 
			
		||||
from gnpy.api.model.error import Error
 | 
			
		||||
 | 
			
		||||
_reaesc = re.compile(r'\x1b[^m]*m')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def common_error_handler(exception):
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    :type exception: Exception
 | 
			
		||||
 | 
			
		||||
    """
 | 
			
		||||
    status_code = 500
 | 
			
		||||
    if not isinstance(exception, werkzeug.exceptions.HTTPException):
 | 
			
		||||
        exception = werkzeug.exceptions.InternalServerError()
 | 
			
		||||
        exception.description = "Something went wrong on our side."
 | 
			
		||||
    response = Error(message=exception.name, description=exception.description,
 | 
			
		||||
                     code=exception.code)
 | 
			
		||||
 | 
			
		||||
    return werkzeug.Response(response=json.dumps(response.__dict__), status=status_code, mimetype='application/json')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def bad_request_handler(exception):
 | 
			
		||||
    response = Error(message='bad request', description=_reaesc.sub('', str(exception)),
 | 
			
		||||
                     code=400)
 | 
			
		||||
    return werkzeug.Response(response=json.dumps(response.__dict__), status=400, mimetype='application/json')
 | 
			
		||||
		Reference in New Issue
	
	Block a user