Adding error codes
This commit is contained in:
parent
7a695b2045
commit
e9838a72e9
@ -1,6 +1,6 @@
|
|||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
const Utils = require('../utils');
|
const Utils = require('../utils/utils');
|
||||||
const utf8 = require('utf8');
|
const utf8 = require('utf8');
|
||||||
const DebugInfo = require('../models/DebugInfo');
|
const DebugInfo = require('../models/DebugInfo');
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
const DebugInfo = require('../models/DebugInfo');
|
const DebugInfo = require('../models/DebugInfo');
|
||||||
const Vehicle = require('../models/vehicle');
|
const Vehicle = require('../models/vehicle');
|
||||||
const Utils = require('../utils');
|
const Utils = require('../utils/utils');
|
||||||
|
|
||||||
const baseUrl = 'https://vin01.ru/v2';
|
const baseUrl = 'https://vin01.ru/v2';
|
||||||
const reportBaseUrl = `${baseUrl}/gibddApp.php`;
|
const reportBaseUrl = `${baseUrl}/gibddApp.php`;
|
||||||
|
|||||||
5
index.js
5
index.js
@ -34,6 +34,11 @@ app.use('/user', users);
|
|||||||
app.use('/vehicles', vehicles);
|
app.use('/vehicles', vehicles);
|
||||||
app.use('/events', events);
|
app.use('/events', events);
|
||||||
|
|
||||||
|
// 404 for all unknown routes
|
||||||
|
app.use((req, res) => {
|
||||||
|
res.status(404).send({ success: false, error: 'Route not found' });
|
||||||
|
});
|
||||||
|
|
||||||
if(process.env.NODE_ENV == 'production') {
|
if(process.env.NODE_ENV == 'production') {
|
||||||
const httpsServer = https.createServer({
|
const httpsServer = https.createServer({
|
||||||
key: fs.readFileSync(process.env.PRIVATE_KEY_PATH),
|
key: fs.readFileSync(process.env.PRIVATE_KEY_PATH),
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
const Constants = require('../data_providers/constants');
|
const Constants = require('../data_providers/constants');
|
||||||
const Utils = require('../utils');
|
const Utils = require('../utils/utils');
|
||||||
const DebugInfo = require('./DebugInfo');
|
const DebugInfo = require('./DebugInfo');
|
||||||
|
|
||||||
class Vehicle {
|
class Vehicle {
|
||||||
|
|||||||
3168
package-lock.json
generated
3168
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,7 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const { v4: uuidv4 } = require('uuid');
|
const { v4: uuidv4 } = require('uuid');
|
||||||
const Utils = require('../utils');
|
const Utils = require('../utils/utils');
|
||||||
|
|
||||||
const makeError = error => ({ success: false, error });
|
const makeError = error => ({ success: false, error });
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,7 @@ const express = require('express');
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const jwt = require('jsonwebtoken');
|
const jwt = require('jsonwebtoken');
|
||||||
const User = require('../models/user');
|
const User = require('../models/user');
|
||||||
|
const { errorCodes, makeError } = require('../utils/errors');
|
||||||
const makeError = error => ({ success: false, error });
|
|
||||||
|
|
||||||
router.post('/signup', async (req, res) => {
|
router.post('/signup', async (req, res) => {
|
||||||
const { email, password } = req.body;
|
const { email, password } = req.body;
|
||||||
@ -37,21 +36,21 @@ router.post('/login', async (req, res) => {
|
|||||||
if(me) {
|
if(me) {
|
||||||
me = User.fromDB(me);
|
me = User.fromDB(me);
|
||||||
if(!me.checkPassword(password)) {
|
if(!me.checkPassword(password)) {
|
||||||
res.send(makeError('Incorrect email or password'));
|
res.send(makeError('Incorrect email or password', errorCodes.invalidLoginOrPassword));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
me.token = jwt.sign({ email }, '#IWantToBelieve', { expiresIn: '365d' });
|
me.token = jwt.sign({ email }, '#IWantToBelieve', { expiresIn: '365d' });
|
||||||
res.send({ success: true, data: me });
|
res.send({ success: true, data: me });
|
||||||
} else {
|
} else {
|
||||||
res.send(makeError('Incorrect login or password'));
|
res.send(makeError('Incorrect login or password', errorCodes.invalidLoginOrPassword));
|
||||||
}
|
}
|
||||||
} catch(ex) {
|
} catch(ex) {
|
||||||
res.send(makeError('Error logging in'));
|
res.send(makeError('Error logging in'));
|
||||||
console.error(ex);
|
console.error(ex);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res.send(makeError('Invalid parameters'));
|
res.status(400).send(makeError('Invalid parameters'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -64,7 +63,7 @@ router.post('/signIn', async (req, res) => {
|
|||||||
if(me) {
|
if(me) {
|
||||||
me = User.fromDB(me);
|
me = User.fromDB(me);
|
||||||
if(!me.checkPassword(password)) {
|
if(!me.checkPassword(password)) {
|
||||||
res.send(makeError('Incorrect email or password'));
|
res.send(makeError('Incorrect email or password', errorCodes.invalidLoginOrPassword));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +80,7 @@ router.post('/signIn', async (req, res) => {
|
|||||||
console.error(ex);
|
console.error(ex);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res.send(makeError('Invalid parameters'));
|
res.status(400).send(makeError('Invalid parameters'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@ const Vin01Provider = require('../data_providers/vin01');
|
|||||||
const { regions } = require('../data_providers/constants');
|
const { regions } = require('../data_providers/constants');
|
||||||
const RsaProvider = require('../data_providers/rsa');
|
const RsaProvider = require('../data_providers/rsa');
|
||||||
const NomerogramProvider = require('../data_providers/nomerogram');
|
const NomerogramProvider = require('../data_providers/nomerogram');
|
||||||
const Utils = require('../utils');
|
const Utils = require('../utils/utils');
|
||||||
const ObjectId = require('mongodb').ObjectID;
|
const ObjectId = require('mongodb').ObjectID;
|
||||||
const DebugInfo = require('../models/DebugInfo');
|
const DebugInfo = require('../models/DebugInfo');
|
||||||
|
|
||||||
|
|||||||
30
utils/errors.js
Normal file
30
utils/errors.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
class Enum {
|
||||||
|
constructor(enumObj) {
|
||||||
|
const handler = {
|
||||||
|
get(target, name) {
|
||||||
|
if (typeof target[name] != 'undefined') {
|
||||||
|
return target[name];
|
||||||
|
}
|
||||||
|
throw new Error(`No such enumerator: ${name}`);
|
||||||
|
},
|
||||||
|
set() {
|
||||||
|
throw new Error('Cannot add/update properties on an Enum instance after it is defined');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return new Proxy(enumObj, handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const errorCodes = new Enum({
|
||||||
|
invalidLoginOrPassword: 0
|
||||||
|
});
|
||||||
|
|
||||||
|
const makeError = (error, code) => {
|
||||||
|
let result = { success: false, error };
|
||||||
|
if(code != undefined) {
|
||||||
|
result.errorCode = code;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = { errorCodes, makeError };
|
||||||
Loading…
Reference in New Issue
Block a user