<commit_before>var fortune = require('../../lib/fortune');
var JSONProblemError = require('../../lib/jsonapi-error');
var RSVP = require('rsvp');

function createApp(options) {

    var fortuneApp = fortune(options)

        .resource('person', {
            name: String,
            appearances: Number,
            pets: ['pet'],
            soulmate: {ref: 'person', inverse: 'soulmate'},
            lovers: [
                {ref: 'person', inverse: 'lovers'}
            ]
        })

        .resource('pet', {
            name: String,
            appearances: Number,
            owner: 'person'
        })

        .resource('foobar', {
            foo: String
        })
        .before(
        function (req, res) {
            var foobar = this;

            if (foobar.foo && foobar.foo === 'bar') {
                // promise
                return new RSVP.Promise(function (resolve, reject) {
                    reject(new JSONProblemError({
                        status: 400,
                        detail: 'Foo was bar'
                    }));
                });
            } else if (foobar.foo && foobar.foo === 'baz') {
                // non-promise
                throw new JSONProblemError({
                    status: 400,
                    detail: 'Foo was baz'
                });
            }
            else {
                return foobar;
            }
        }
    );


    fortuneApp.router.get('/random-error', function (req, res, next) {
        next(new Error('this is an error'));
    });

    fortuneApp.router.get('/json-errors-error', function (req, res, next) {
        next(new JSONProblemError({status: 400, detail: 'Bar was not foo'}));
    });


    return RSVP.all([
            fortuneApp.onRouteCreated('pet'),
            fortuneApp.onRouteCreated('person'),
            fortuneApp.onRouteCreated('foobar')
        ])
        .then(function () {
            return fortuneApp;
        });
}

module.exports = createApp;<commit_msg>using http-as-promised, composes promises more efficiently and has a nice error model<commit_after>var fortune = require('../../lib/fortune');
var JSONAPI_Error = require('../../lib/jsonapi-error');
var RSVP = require('rsvp');

function createApp(options) {

    var fortuneApp = fortune(options)

        .resource('person', {
            name: String,
            appearances: Number,
            pets: ['pet'],
            soulmate: {ref: 'person', inverse: 'soulmate'},
            lovers: [
                {ref: 'person', inverse: 'lovers'}
            ]
        })

        .resource('pet', {
            name: String,
            appearances: Number,
            owner: 'person'
        })

        .resource('foobar', {
            foo: String
        })
        .before(
        function (req, res) {
            var foobar = this;

            if (foobar.foo && foobar.foo === 'bar') {
                // promise
                return new RSVP.Promise(function (resolve, reject) {
                    reject(new JSONAPI_Error({
                        status: 400,
                        detail: 'Foo was bar'
                    }));
                });
            } else if (foobar.foo && foobar.foo === 'baz') {
                // non-promise
                throw new JSONAPI_Error({
                    status: 400,
                    detail: 'Foo was baz'
                });
            }
            else {
                return foobar;
            }
        }
    );


    fortuneApp.router.get('/random-error', function (req, res, next) {
        next(new Error('this is an error'));
    });

    fortuneApp.router.get('/json-errors-error', function (req, res, next) {
        next(new JSONAPI_Error({status: 400, detail: 'Bar was not foo'}));
    });


    return RSVP.all([
            fortuneApp.onRouteCreated('pet'),
            fortuneApp.onRouteCreated('person'),
            fortuneApp.onRouteCreated('foobar')
        ])
        .then(function () {
            return fortuneApp;
        });
}

module.exports = createApp;
{'fraction_non_alphanumeric': 0.13394178589047853, 'fraction_numerical': 0.004440059200789344, 'mean_word_length': 1.363053613053613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5174997', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1061, 'n_words': 244}