Start a new topic

How can I inspect errors thrown by the library?

Something goes wrong inside my code, but I can’t seem to figure out why. Is there a way to get the error from the library?


Yes, there are multiple ways to gain insight in what the library is doing and where things might go wrong.



1. Inspect the error object.



Almost all methods return an error object when an error is encountered. For example:



var promise = Kinvey.User.login('username', 'incorrect-password');

promise.then(function(user) {

// The user is now logged in.

}, function(error) {

// An error occured, prompt it.

alert(JSON.stringify(error));

console.log(error); // Or, log it.

});



The `error` variable in the example above consists of three properties:



* `name`: the error name.

* `description`: description of the error.

* `debug`: any hints towards the cause or solution of the error.



2. Enable `KINVEY_DEBUG` mode.



The KINVEY_DEBUG mode is available in the non-minified version of the library. To enable it, simply execute the following line of code:



var KINVEY_DEBUG = true;



In the console window of your browser or debugging tool, the library logs information about what it is exactly doing.
This is the way promises behave, you can catch "final" errors by adding a final `then` statement:



Kinvey.DataStore.find('collection', null).then(function (response) {

somethingUndefined;

console.log(response);

}).then(null, function(error) {

// error will be the "somethingUndefined is not defined" error

});
Login or Signup to post a comment