Cleaning the error system
This commit is contained in:
1 parent
734e158347
commit
d3f0a610c1
9 files changed
+54
-18
No files matched your search
@@ -9,6 +9,11 @@
|
||||
"start": "nodemon --exec babel-node --presets env,stage-2 src/index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"standard": {
|
||||
"ignore": [
|
||||
"/src/errors/index.js"
|
||||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.26.0",
|
||||
"babel-preset-env": "^1.7.0",
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
export class AuthentificationError extends Error {
|
||||
code = 401
|
||||
message = 'You have to be authentificated to ' + ( this.message || "perform this action")
|
||||
message = 'Autentification not found, you have to be authentificated to perform this action'
|
||||
}
|
||||
|
||||
export class DatabaseError extends Error {
|
||||
code = 404
|
||||
message = 'Ressource not found : ' + this.message
|
||||
}
|
||||
|
||||
export class WrongArgumentsError extends Error {
|
||||
code = 400
|
||||
message = 'Wrong arguments : ' + this.message
|
||||
}
|
||||
@@ -3,7 +3,11 @@ import {
|
||||
GraphQLID,
|
||||
GraphQLInt
|
||||
} from 'graphql'
|
||||
import { AuthentificationError } from '../../errors'
|
||||
import {
|
||||
AuthentificationError,
|
||||
DatabaseError,
|
||||
WrongArgumentsError
|
||||
} from '../../errors'
|
||||
import { EventType } from '../types'
|
||||
import { Event } from '../../models'
|
||||
|
||||
@@ -20,12 +24,13 @@ const resolve = (parent, { eventDesignator, rank, amount }, context) => {
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => {
|
||||
if (err || !event) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else if (!event) {
|
||||
reject(new DatabaseError('None of the events you own respond to that designator'))
|
||||
} else {
|
||||
if (event.prizes.some(prize => prize.rank === rank)) {
|
||||
// need to add an error message here TODO
|
||||
resolve(event)
|
||||
reject(new WrongArgumentsError('A prize for that rank already exists'))
|
||||
} else {
|
||||
event.prizes.push({
|
||||
rank,
|
||||
|
||||
@@ -3,7 +3,11 @@ import {
|
||||
GraphQLID,
|
||||
GraphQLString
|
||||
} from 'graphql'
|
||||
import { AuthentificationError } from '../../errors'
|
||||
import {
|
||||
AuthentificationError,
|
||||
DatabaseError,
|
||||
WrongArgumentsError
|
||||
} from '../../errors'
|
||||
import { EventType } from '../types'
|
||||
import { Event } from '../../models'
|
||||
|
||||
@@ -20,11 +24,13 @@ const resolve = (parent, { eventDesignator, title, description }, context) => {
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => {
|
||||
if (err || !event) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else if (!event) {
|
||||
reject(new DatabaseError('None of the events you own respond to that designator'))
|
||||
} else {
|
||||
if (event.rules.some(rule => rule.title === title)) {
|
||||
resolve(event)
|
||||
reject(new WrongArgumentsError('A prize for that rank already exists'))
|
||||
} else {
|
||||
let position = 1
|
||||
while (event.rules.some(rule => rule.position === position)) {
|
||||
|
||||
@@ -2,7 +2,7 @@ import {
|
||||
GraphQLNonNull,
|
||||
GraphQLString
|
||||
} from 'graphql'
|
||||
import { AuthentificationError } from '../../errors'
|
||||
import { AuthentificationError, DatabaseError } from '../../errors'
|
||||
import { EventType } from '../types'
|
||||
import { Event, Formula } from '../../models'
|
||||
|
||||
@@ -19,8 +19,10 @@ const resolve = (parent, { name, eventDesignator }, context) => {
|
||||
let newFormula = new Formula({ name, ownerId })
|
||||
return new Promise((resolve, reject) => {
|
||||
Event.findOne({ designator: eventDesignator, hostId: ownerId }).exec((err, event) => {
|
||||
if (err || !event) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else if (!event) {
|
||||
reject(new DatabaseError('None of the events you own respond to that designator'))
|
||||
} else {
|
||||
newFormula.save((err) => {
|
||||
if (err) reject(err)
|
||||
|
||||
@@ -2,7 +2,7 @@ import {
|
||||
GraphQLNonNull,
|
||||
GraphQLString
|
||||
} from 'graphql'
|
||||
import { AuthentificationError } from '../../errors'
|
||||
import { AuthentificationError, DatabaseError } from '../../errors'
|
||||
import { EventType } from '../types'
|
||||
import { Event, Plotter } from '../../models'
|
||||
|
||||
@@ -22,8 +22,10 @@ const resolve = (parent, { name, host, repo, moduleName, eventDesignator }, cont
|
||||
let newPlotter = new Plotter({ name, host, repo, moduleName, ownerId })
|
||||
return new Promise((resolve, reject) => {
|
||||
Event.findOne({ designator: eventDesignator, hostId: ownerId }).exec((err, event) => {
|
||||
if (err || !event) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else if (!event) {
|
||||
reject(new DatabaseError('None of the events you own respond to that designator'))
|
||||
} else {
|
||||
newPlotter.save((err) => {
|
||||
if (err) reject(err)
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
GraphQLInt,
|
||||
GraphQLString
|
||||
} from 'graphql'
|
||||
import { AuthentificationError } from '../../errors'
|
||||
import { AuthentificationError, DatabaseError } from '../../errors'
|
||||
import { EventType } from '../types'
|
||||
import { Event } from '../../models'
|
||||
|
||||
@@ -25,8 +25,10 @@ const resolve = (parent, { designator, name, description, startDatetime, finishD
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
Event.findOne({ designator, hostId }).exec((err, event) => {
|
||||
if (err || !event) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else if (!event) {
|
||||
reject(new DatabaseError('None of the events you own respond to that designator'))
|
||||
} else {
|
||||
event.name = name || event.name
|
||||
event.description = description || event.description
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
GraphQLID,
|
||||
GraphQLString
|
||||
} from 'graphql'
|
||||
import { AuthentificationError } from '../../errors'
|
||||
import { AuthentificationError, DatabaseError } from '../../errors'
|
||||
import { EventType } from '../types'
|
||||
import { Event } from '../../models'
|
||||
|
||||
@@ -21,8 +21,10 @@ const resolve = (parent, { eventDesignator, title, newtitle, description }, cont
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => {
|
||||
if (err || !event) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else if (!event) {
|
||||
reject(new DatabaseError('None of the events you own respond to that designator'))
|
||||
} else {
|
||||
let ruleIndex
|
||||
event.rules.find((rule, index) => {
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
GraphQLID,
|
||||
GraphQLInt
|
||||
} from 'graphql'
|
||||
import { AuthentificationError } from '../../errors'
|
||||
import { AuthentificationError, DatabaseError } from '../../errors'
|
||||
import { isBetween } from '../../utils/functions'
|
||||
import { EventType } from '../types'
|
||||
import { Event } from '../../models'
|
||||
@@ -21,8 +21,10 @@ const resolve = (parent, { eventDesignator, fromPosition, toPosition }, context)
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => {
|
||||
if (err || !event) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else if (!event) {
|
||||
reject(new DatabaseError('None of the events you own respond to that designator'))
|
||||
} else {
|
||||
const delta = fromPosition > toPosition ? 1 : -1
|
||||
event.rules.forEach((part) => {
|
||||
|
||||
Reference in new issue
Block a user