<s> from tornado . web import RequestHandler <EOL> from jsonschema import ValidationError <EOL> from tornado_json . jsend import JSendMixin <EOL> from tornado_json . exceptions import APIError <EOL> class BaseHandler ( RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> __url_names__ = [ "<STR_LIT>" ] <EOL> __urls__ = [ ] <EOL> @ property <EOL> def db_conn ( self ) : <EOL> """<STR_LIT>""" <EOL> db_conn = self . application . db_conn <EOL> if not db_conn : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> return db_conn <EOL> class ViewHandler ( BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> def initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> self . set_header ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> class APIHandler ( BaseHandler , JSendMixin ) : <EOL> """<STR_LIT>""" <EOL> def initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> self . set_header ( "<STR_LIT:Content-Type>" , "<STR_LIT:application/json>" ) <EOL> def write_error ( self , status_code , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def get_exc_message ( exception ) : <EOL> return exception . log_message if hasattr ( exception , "<STR_LIT>" ) else str ( exception ) <EOL> self . clear ( ) <EOL> self . set_status ( status_code ) <EOL> exception = kwargs [ "<STR_LIT>" ] [ <NUM_LIT:1> ] <EOL> if any ( isinstance ( exception , c ) for c in [ APIError , ValidationError ] ) : <EOL> if isinstance ( exception , ValidationError ) : <EOL> self . set_status ( <NUM_LIT> ) <EOL> self . fail ( get_exc_message ( exception ) ) <EOL> else : <EOL> self . error ( <EOL> message = self . _reason , <EOL> data = get_exc_message ( exception ) if self . settings . get ( "<STR_LIT>" ) <EOL> else None , <EOL> code = status_code <EOL> ) </s>
