Best Python code snippet using localstack_python
error_handler.py
Source:error_handler.py
1# -*- coding: utf-8 -*-2from flask import jsonify3from sqlalchemy.exc import IntegrityError, OperationalError, InternalError4from sqlalchemy.exc import DataError5from server_api.database import db6from main import app7import http8import luida_server.status as STATUS9TARGET_HTTP_ERROR_CODES = (10 http.client.BAD_REQUEST,11 http.client.UNAUTHORIZED,12 http.client.FORBIDDEN,13 http.client.NOT_FOUND,14 http.client.METHOD_NOT_ALLOWED,15 http.client.NOT_ACCEPTABLE,16 http.client.REQUEST_TIMEOUT,17 http.client.CONFLICT,18 http.client.GONE,19 http.client.LENGTH_REQUIRED,20 http.client.PRECONDITION_FAILED,21 http.client.REQUEST_ENTITY_TOO_LARGE,22 http.client.REQUEST_URI_TOO_LONG,23 http.client.UNSUPPORTED_MEDIA_TYPE,24 http.client.REQUESTED_RANGE_NOT_SATISFIABLE,25 http.client.EXPECTATION_FAILED,26 http.client.INTERNAL_SERVER_ERROR,27 http.client.NOT_IMPLEMENTED,28 http.client.BAD_GATEWAY,29 http.client.SERVICE_UNAVAILABLE,30 http.client.GATEWAY_TIMEOUT,31 http.client.HTTP_VERSION_NOT_SUPPORTED32)33def error_handler(err):34 """error to JSON format"""35 error_response = jsonify(36 status=getattr(err, 'code', http.client.INTERNAL_SERVER_ERROR),37 name=getattr(err, 'name', 'Unkonwn'),38 message=getattr(err, 'description', '')39 )40 return error_response41for error in TARGET_HTTP_ERROR_CODES:42 app.register_error_handler(error, error_handler)43@app.errorhandler(OperationalError)44def handle_db_operational_err(err):45 error_response = jsonify(46 status=STATUS.DB_OPERATION_ERR,47 name='Database operational error',48 message=str(err)49 )50 db.session.rollback()51 return error_response52@app.errorhandler(IntegrityError)53def handle_db_integrity_err(err):54 error_response = jsonify(55 status=STATUS.DUPLICATE_ERR,56 name='Database integrity error',57 # message=str(err)58 )59 db.session.rollback()60 return error_response61@app.errorhandler(AttributeError)62def handle_attribute_err(err):63 error_response = jsonify(64 status=http.client.INTERNAL_SERVER_ERROR,65 name='Attrubute error',66 message=str(err)67 )68 db.session.rollback()69 return error_response70@app.errorhandler(InternalError)71def handle_db_internal_err(err):72 error_response = jsonify(73 status=STATUS.UNKOWN_COLUMN_ERR,74 name='Unknown column error',75 # message=str(err)76 )77 db.session.rollback()78 return error_response79@app.errorhandler(DataError)80def handle_db_data_err(err):81 error_response = jsonify(82 status=STATUS.TOO_LONG_CULUMN_ERR,83 name='Data too long for column',84 # message=str(err)85 )86 db.session.rollback()...
views.py
Source:views.py
...7 Same as oauth2_provider.views.AuthorizationView, except8 that error_response doesn't add a 'url' variable to the context,9 overriding the 'url' reverse function.10 """11 def error_response(self, error, **kwargs):12 """Handle errors with a redirect or an error response."""13 redirect, error_response = super(14 BaseAuthorizationView, self).error_response(error, **kwargs)15 if redirect:16 return HttpResponseUriRedirect(error_response['url'])17 status = error_response['error'].status_code18 error_response.pop('url', None)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!