day 11
This commit is contained in:
Executable
+46
@@ -0,0 +1,46 @@
|
||||
require('dotenv').config();
|
||||
|
||||
const util = require('util');
|
||||
const graphql = require('apollo-server-express');
|
||||
const sequelize = require('sequelize');
|
||||
|
||||
util.inspect.defaultOptions.depth = null;
|
||||
|
||||
exports.formatError = (error, options = { showHiddenNodes: false }) => {
|
||||
util.inspect.defaultOptions.showHidden = options.showHiddenNodes;
|
||||
console.log('FORMAT ERROR', { error });
|
||||
if (error instanceof sequelize.ValidationError) {
|
||||
return {
|
||||
success: false,
|
||||
errors: error.errors.map((x) => ({ path: x.path, message: x.message })),
|
||||
code: 'ERROR_DATABASE_VALIDATION',
|
||||
};
|
||||
}
|
||||
|
||||
if (error instanceof graphql.ValidationError) {
|
||||
return {
|
||||
success: false,
|
||||
code: 'ERROR_GRAPHQL_VALIDATION',
|
||||
errors: [{ path: null, message: error.message }],
|
||||
};
|
||||
}
|
||||
|
||||
return { success: false, message: 'Something went wrong' };
|
||||
};
|
||||
|
||||
exports.formatGraphqlError = (error) => {
|
||||
let { extensions, ...rest } = error;
|
||||
|
||||
if ('stacktrace' in extensions?.exception && process.env.MODE !== 'development') {
|
||||
const { stacktrace, ...restException } = extensions.exception;
|
||||
rest = {
|
||||
...rest,
|
||||
extensions: {
|
||||
...extensions,
|
||||
exception: restException,
|
||||
},
|
||||
};
|
||||
return rest;
|
||||
}
|
||||
return error;
|
||||
};
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Generate random code with given length
|
||||
* @param {Number} length code length
|
||||
* @returns {String} Generated code
|
||||
* @example
|
||||
* generateCode(6)
|
||||
*/
|
||||
module.exports = function generateCode(length) {
|
||||
var result = '';
|
||||
var characters = '0123456789';
|
||||
var charactersLength = characters.length;
|
||||
for (var i = 0; i < length; i++) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
}
|
||||
return result;
|
||||
};
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
function capitalizeFirstLetter(target) {
|
||||
if (typeof target !== 'string') {
|
||||
throw new Error('Parameter should be a string.');
|
||||
}
|
||||
return target?.substring(0, 1).toUpperCase() + target?.substring(1)?.toLocaleLowerCase();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
capitalizeFirstLetter,
|
||||
};
|
||||
Reference in New Issue
Block a user