mirror of
https://github.com/koodiklinikka/koodiklinikka.fi-api.git
synced 2026-03-22 16:07:11 +00:00
better error handling
This commit is contained in:
@@ -13,13 +13,17 @@ module.exports = function (app) {
|
|||||||
app.post('/invites', function(req, res, next) {
|
app.post('/invites', function(req, res, next) {
|
||||||
|
|
||||||
if(!validator.isEmail(req.body.email)) {
|
if(!validator.isEmail(req.body.email)) {
|
||||||
res.status(400).send('Invalid email');
|
return res.status(400).send('invalid_email');
|
||||||
}
|
}
|
||||||
|
|
||||||
function success() {
|
function success() {
|
||||||
res.status(200).end();
|
res.status(200).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function alreadyInvited(email) {
|
||||||
|
res.status(400).send('already_invited');
|
||||||
|
}
|
||||||
|
|
||||||
slack
|
slack
|
||||||
.createInvite(req.body.email)
|
.createInvite(req.body.email)
|
||||||
.then(function() {
|
.then(function() {
|
||||||
@@ -37,6 +41,11 @@ module.exports = function (app) {
|
|||||||
})
|
})
|
||||||
.then(success)
|
.then(success)
|
||||||
.catch(function(err) {
|
.catch(function(err) {
|
||||||
|
|
||||||
|
if(err === 'already_invited') {
|
||||||
|
return alreadyInvited(req.body.email);
|
||||||
|
}
|
||||||
|
|
||||||
var message = 'Creating automatic invitation failed for: ' + req.body.email + ' reason: ' + err;
|
var message = 'Creating automatic invitation failed for: ' + req.body.email + ' reason: ' + err;
|
||||||
slack.createMessage(message);
|
slack.createMessage(message);
|
||||||
|
|
||||||
|
|||||||
@@ -49,10 +49,22 @@ module.exports = {
|
|||||||
.query({q: email})
|
.query({q: email})
|
||||||
.set('Authorization', 'token ' + config.github.token)
|
.set('Authorization', 'token ' + config.github.token)
|
||||||
.end(function(error, response){
|
.end(function(error, response){
|
||||||
if (error) {
|
if(error) {
|
||||||
reject(error);
|
return reject(error);
|
||||||
}
|
}
|
||||||
resolve(response.body);
|
|
||||||
|
if(!response.body.items) {
|
||||||
|
return reject(new Error(response.body.message));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(response.body.items.length === 0) {
|
||||||
|
return reject(new Error('Not Found'));
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(response.body.items[0]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* Invite user to organization
|
* Invite user to organization
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user