Adding error codes
This commit is contained in:
parent
7a695b2045
commit
e9838a72e9
@ -1,6 +1,6 @@
|
||||
const crypto = require('crypto');
|
||||
const fetch = require('node-fetch');
|
||||
const Utils = require('../utils');
|
||||
const Utils = require('../utils/utils');
|
||||
const utf8 = require('utf8');
|
||||
const DebugInfo = require('../models/DebugInfo');
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
const fetch = require('node-fetch');
|
||||
const DebugInfo = require('../models/DebugInfo');
|
||||
const Vehicle = require('../models/vehicle');
|
||||
const Utils = require('../utils');
|
||||
const Utils = require('../utils/utils');
|
||||
|
||||
const baseUrl = 'https://vin01.ru/v2';
|
||||
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('/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') {
|
||||
const httpsServer = https.createServer({
|
||||
key: fs.readFileSync(process.env.PRIVATE_KEY_PATH),
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
const Constants = require('../data_providers/constants');
|
||||
const Utils = require('../utils');
|
||||
const Utils = require('../utils/utils');
|
||||
const DebugInfo = require('./DebugInfo');
|
||||
|
||||
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 router = express.Router();
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
const Utils = require('../utils');
|
||||
const Utils = require('../utils/utils');
|
||||
|
||||
const makeError = error => ({ success: false, error });
|
||||
|
||||
|
||||
@ -2,8 +2,7 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
const jwt = require('jsonwebtoken');
|
||||
const User = require('../models/user');
|
||||
|
||||
const makeError = error => ({ success: false, error });
|
||||
const { errorCodes, makeError } = require('../utils/errors');
|
||||
|
||||
router.post('/signup', async (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
@ -37,21 +36,21 @@ router.post('/login', async (req, res) => {
|
||||
if(me) {
|
||||
me = User.fromDB(me);
|
||||
if(!me.checkPassword(password)) {
|
||||
res.send(makeError('Incorrect email or password'));
|
||||
res.send(makeError('Incorrect email or password', errorCodes.invalidLoginOrPassword));
|
||||
return;
|
||||
}
|
||||
|
||||
me.token = jwt.sign({ email }, '#IWantToBelieve', { expiresIn: '365d' });
|
||||
res.send({ success: true, data: me });
|
||||
} else {
|
||||
res.send(makeError('Incorrect login or password'));
|
||||
res.send(makeError('Incorrect login or password', errorCodes.invalidLoginOrPassword));
|
||||
}
|
||||
} catch(ex) {
|
||||
res.send(makeError('Error logging in'));
|
||||
console.error(ex);
|
||||
}
|
||||
} 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) {
|
||||
me = User.fromDB(me);
|
||||
if(!me.checkPassword(password)) {
|
||||
res.send(makeError('Incorrect email or password'));
|
||||
res.send(makeError('Incorrect email or password', errorCodes.invalidLoginOrPassword));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -81,7 +80,7 @@ router.post('/signIn', async (req, res) => {
|
||||
console.error(ex);
|
||||
}
|
||||
} 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 RsaProvider = require('../data_providers/rsa');
|
||||
const NomerogramProvider = require('../data_providers/nomerogram');
|
||||
const Utils = require('../utils');
|
||||
const Utils = require('../utils/utils');
|
||||
const ObjectId = require('mongodb').ObjectID;
|
||||
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