mirror of
https://github.com/tcgdex/cards-database.git
synced 2025-04-24 11:52:16 +00:00
feat(server): Add better logging to detect potential errors
Signed-off-by: Avior <f.bouillon@aptatio.com>
This commit is contained in:
parent
33814d7df2
commit
8192542c1d
@ -1,26 +1,44 @@
|
||||
import express from 'express'
|
||||
import { graphqlHTTP } from 'express-graphql'
|
||||
import { buildSchema, formatError } from 'graphql'
|
||||
import resolver from './resolver'
|
||||
import fs from 'fs'
|
||||
import { buildSchema, formatError, GraphQLError } from 'graphql'
|
||||
import resolver from './resolver'
|
||||
|
||||
// Init Express Router
|
||||
const router = express.Router()
|
||||
|
||||
/**
|
||||
* Drawbacks
|
||||
* Attack.damage is a string instead of possibly being a number or a string
|
||||
*/
|
||||
const schema = buildSchema(fs.readFileSync('./public/v2/graphql.gql').toString())
|
||||
|
||||
// Error Logging for debugging
|
||||
function graphQLErrorHandle(error: GraphQLError) {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
console.error(error)
|
||||
}
|
||||
if (error.source) {
|
||||
console.error('GraphQL Error')
|
||||
console.error(error.message)
|
||||
console.error(error.source?.body.replace(/\n/g, '\\n'))
|
||||
}
|
||||
return formatError(error)
|
||||
}
|
||||
|
||||
// Add graphql to the route
|
||||
router.use(graphqlHTTP({
|
||||
router.get('/', graphqlHTTP({
|
||||
schema,
|
||||
rootValue: resolver,
|
||||
graphiql: true,
|
||||
customFormatErrorFn(error) {
|
||||
console.error(error)
|
||||
return formatError(error)
|
||||
}
|
||||
customFormatErrorFn: graphQLErrorHandle
|
||||
}))
|
||||
|
||||
router.post('/', graphqlHTTP({
|
||||
schema,
|
||||
rootValue: resolver,
|
||||
graphiql: true,
|
||||
customFormatErrorFn: graphQLErrorHandle
|
||||
}))
|
||||
|
||||
export default router
|
||||
|
@ -1,14 +1,14 @@
|
||||
import express from 'express'
|
||||
import graphql from './V2/graphql'
|
||||
import jsonEndpoints from './V2/endpoints/jsonEndpoints'
|
||||
import status from './status'
|
||||
import jsonEndpoints from './V2/endpoints/jsonEndpoints'
|
||||
import graphql from './V2/graphql'
|
||||
// Current API version
|
||||
const VERSION = 2
|
||||
|
||||
// Init Express server
|
||||
const server = express()
|
||||
|
||||
// Set global headers
|
||||
// Set CORS global headers
|
||||
server.use((_, res, next) => {
|
||||
res
|
||||
.setHeader('Access-Control-Allow-Origin', '*')
|
||||
@ -18,6 +18,19 @@ server.use((_, res, next) => {
|
||||
next()
|
||||
})
|
||||
|
||||
// Route logging / Error logging for debugging
|
||||
server.use((req, res, next) => {
|
||||
res.on('close', () => {
|
||||
console.log(`[${new Date().toISOString()}]: ${req.method.padStart(7, ' ')} ${res.statusCode} ${(req.baseUrl ?? '') + req.url}`)
|
||||
})
|
||||
res.on('error', (err) => {
|
||||
console.error('Error:')
|
||||
console.error(err)
|
||||
})
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
server.get('/', (_, res) => {
|
||||
res.redirect('https://www.tcgdex.dev/?ref=api.tcgdex.net')
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user