<commit_before>'use strict';

const Validator = require( './validator' );

/**
 * @Promise
 */
function simpleExecutor( handler, event, handlerContext ) {

    try {

        return handler.call( handlerContext, event );
    }
    catch( err ) {

        let error = new Error( 'Uncaught exception: ' + err.message );
        error.cause = err;

        throw error;
    }
}

/**
 * @Promise
 */
function asyncExecutor( handler, event, handlerContext ) {

    return new Promise( (resolve, reject) => {

        try {

            handler.call( handlerContext, event, ( err, result ) => {

                if( err ) {

                    return reject( err );
                }

                resolve( result );
            });
        }
        catch( err ) {

            let error = new Error( 'Uncaught exception: ' + err.message );
            error.cause = err;

            reject( error );
        }
    })
}

class MethodHandler {

    constructor( handler, options ) {

        this.handler = handler;

        if( handler.length === 1 ) {

            this.executor = simpleExecutor;
        }
        else {

            this.executor = asyncExecutor;
        }

        this.validator = new Validator( options );
    }

    validate( event ) {

        this.validator.validate( event );
    }

    /**
     * @Promise
     */
    execute( event, context ) {

        let handlerContext = {

            event,
            context<commit_msg>Changed to use UncaughtError<commit_after>'use strict';

const Validator = require( './validator' );

const UncaughtError = require( '../../errors' ).UncaughtError;

/**
 * @Promise
 */
function simpleExecutor( handler, event, handlerContext ) {

    try {

        return handler.call( handlerContext, event );
    }
    catch( err ) {

        throw new UncaughtError( err );
    }
}

/**
 * @Promise
 */
function asyncExecutor( handler, event, handlerContext ) {

    return new Promise( (resolve, reject) => {

        try {

            handler.call( handlerContext, event, ( err, result ) => {

                if( err ) {

                    return reject( err );
                }

                resolve( result );
            });
        }
        catch( err ) {

            reject( new UncaughtError( err ) );
        }
    })
}

class MethodHandler {

    constructor( handler, options ) {

        this.handler = handler;

        if( handler.length === 1 ) {

            this.executor = simpleExecutor;
        }
        else {

            this.executor = asyncExecutor;
        }

        this.validator = new Validator( options );
    }

    /**
     * @Promise
     */
    execute( event, context ) {

        this.validator.validate( event );

        let handlerContext = {

            event,
            context
{'fraction_non_alphanumeric': 0.11775232641374374, 'fraction_numerical': 0.0007158196134574087, 'mean_word_length': 1.522563176895307, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4013617', 'n_tokens_mistral': 782, 'n_tokens_neox': 733, 'n_words': 187}