Base setup

This commit is contained in:
Barry Low committed 2018-12-07 19:14:14 -08:00
commit beeccd7e3d
40 files changed
+7288

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
export class AuthenticationError extends Error {
code = 401
message = 'Not Authenticated - you have to be authenticated to perform this action'
}
export class DatabaseError extends Error {
code = 404
message = `Resource Error: ${this.message}`
}
export class ApolloError extends Error {
code = 409
message = `Resource already exists: ${this.message}`
}
export class WrongArgumentsError extends Error {
code = 400
message = 'Wrong arguments : ' + this.message
}
export class ServiceUnavailableError extends Error {
code = 503
message = 'At least one service is unresponding ' + this.message
}
+33
View File
@@ -0,0 +1,33 @@
import { createLogger, format, transports } from 'winston'
import { inspect } from 'util'
import { AuthenticationError, ApolloError, DatabaseError, WrongArgumentsError, ServiceUnavailableError, ConflictError } from './errors'
class LoggerService {
constructor() {
const winstonTransports = [
new transports.Console({
format: format.combine(
format.colorize(),
format.timestamp({ format: 'YY/MM/DD HH:mm' }),
format.printf(
info => `${info.timestamp} - ${info.level}: ${JSON.parse(JSON.stringify(inspect(info.message)))}`
)
),
}),
]
const logger = createLogger({
level: 'info',
transports: winstonTransports,
exceptionHandlers: winstonTransports,
})
return new Proxy(this, {
get: (target, propKey) => logger[propKey],
})
}
}
export const logger = new LoggerService() // Default logger
export { LoggerService, AuthenticationError, ApolloError, ConflictError, DatabaseError, WrongArgumentsError, ServiceUnavailableError }