diff --git a/js/ui/cordova/plugins/cordova-plugin-chrome-apps-common/errors.js b/js/ui/cordova/plugins/cordova-plugin-chrome-apps-common/errors.js new file mode 100644 index 0000000..403b5c0 --- /dev/null +++ b/js/ui/cordova/plugins/cordova-plugin-chrome-apps-common/errors.js @@ -0,0 +1,42 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +try { +var runtime = require('cordova-plugin-chrome-apps-runtime.runtime'); +} catch(e) {} + +// Typical Usage: +// +// if (fail_condition) +// return callbackWithError('You should blah blah', fail, optional_args_to_fail...) +function callbackWithError(msg, callback) { + var err; + if (typeof msg == 'string') { + err = { 'message' : msg }; + } else { + err = msg; + } + + if (typeof callback !== 'function') { + console.error(err.message); + return; + } + + try { + if (typeof runtime !== 'undefined') { + runtime.lastError = err; + } else { + console.error(err.message); + } + + callback.apply(null, Array.prototype.slice.call(arguments, 2)); + } finally { + if (typeof runtime !== 'undefined') + delete runtime.lastError; + } +} + +module.exports = { + callbackWithError: callbackWithError +};