How To Catch Errors When Adding a Field With Knex.JS
<p>We're using knex.js to generate and execute SQL. We're having trouble trapping errors when adding fields to an existing table and the <a href="http://knexjs.org/#Schema" rel="nofollow">documentation</a> does not cover this specific use-case. Attempting to add a duplicate field does return an error in the console (VSCODE), but none of standard events are called on an error so we can't trap it in code. Here's the code:</p>

<pre><code>knex.schema.table(tableName, function(table) {

    table.string('test').catch(function(e) {
        callback(e)
    });

}).then(function(e) {
    callback(e);
}).catch(function(e) {
    callback(e);
})
</code></pre>

<p>This is returned in the VSCODE console:</p>

<pre><code>{ [TypeError: table.string(...).catch is not a function] 'error@context': { accessToken: undefined } }
</code></pre>

<p>However, none of the callbacks are called. How can we check for errors when adding fields?</p>

<p><strong>UPDATE #1</strong> This code will call the callback, but obviously without any error information. And, the same error appears in the console regardless:</p>

<pre><code>table.string('test').catch(
    callback(null, Lib.returnEvent(false))
);
</code></pre>

<p><strong>UPDATE #2</strong> No callbacks are called with the following code:</p>

<pre><code>knex.schema.table(tableName, function(table) {
    table.string('ddd');
}).then(function(e) {
    callback(e);
}).catch(function(e) {
    callback(e);
})
</code></pre>

<p><strong>UPDATE #3</strong> In this example the first callback is called, but the function hangs on subsequent calls:</p>

<pre><code>knex.schema.table(tableName, function(table) {
    table.string('ddd');
    callback(true);
}).then(function(e) {
    callback(true, e);
}).catch(function(e) {
    callback(false, e);
})
</code></pre>