Skip to content

Errors handling

AGenson edited this page Mar 4, 2018 · 1 revision

Errors handling

Each operation functions return the wanted information, with a specific format (name, message, data).

But they may encounters errors. And the error format is the same as for normal answers: name, message(, data)

Here's a little description of how they are handled.


Parameter Error

Most of parameter error are handled, and the format will be either:

Promise.reject({
	name: "Missing parameter",
	message: `Missing parameter ${item}`
});

or

Promise.reject({
	name: "Invalid parameter",
	message: `Invalid parameter ${item}...`
});

Error on Query, Update & Filter

When sending a query, an update or setting a filter, all the fields need to correspond to the column of the table. So errors can be both :

Promise.reject({
	name: "Invalid data type",
	message: `Data type must be (Object|String[])`
});

or

Promise.reject({
	name: "Invalid field",
	message: `Invalid field ${result.field} for ${result.type}` // Type = query or filter
});

Database Error

Not all database errors are handled. Normally if one not handled occure, it might come from the database and not the request itself.

In this case, the error name will always be "Database Error".

Default

The adapter use Sequelize to interract with the database, so it will be Sequelize Error. If it is a matter of field value ('notNull', 'unique', or else):

Promise.reject({
	name: "Database Error",
	message: message,
	data: errors
});

Details

The message can be either:

  • "NOT NULL contraint not respected"
  • "UNIQUE contraint not respected"
  • "Unknown error"

And the data (if it exists) will be an array of the error per field (ex: { field: "username", type: "unique" }). Types can be:

  • "notNull"
  • "unique"
  • else (a Sequelize type)

Else

Else you will have the SQL message returned by Sequelize to help you. If the error is still unknown ("Unknown Error"), there might be a connection problem with the database.

Make sure you are giving the right parameters for the function, especially data types of the parameters / fields.


Search Error

When searching for entities, the function will automatically throw an error if nothing is found, except for count.

Promise.reject({
	name: "Nothing Found",
	message: `Search Complete: 0 element found`
});

Unknown Error

Something anormally went wrong. Please check your parameters. Else it might from the database side.

Promise.reject({
	name: "Unkown Error",
	message: "Internal Error: something went wrong while ${action} for entities"
});