fastify / fastify-circuit-breaker

A low overhead circuit breaker for your routes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how i make Custom Reply :(

iibrahem opened this issue · comments

💬 how i make Custom Reply

I'm checked this method [(fastify.setErrorHandler) ] but not helpful its work after threshold limit but in (TimeoutError) i can't make custom reply.

i need help for this problem.

And Thanks for this beautiful library ^_^

I would recommend to make a full example of what it's currently not working.
Even better: open a PR to fix it!

Ok this is my example :

const Fastify = require('fastify')
const fastify = Fastify()

// that Handler helped me for catch CircuitOpenError but it can't catch the TimeoutError
fastify.setErrorHandler(function (_error, request, reply) {
	console.log('================^^^^^====================')
	console.log(_error)
	console.log('=========================================')
	return reply.send({ ERRRROR: 404 })
})

fastify.register(require('fastify-circuit-breaker'), {
	threshold: 2,
	timeout: 3000,
	resetTimeout: 30000,

	// maby if add arg like taht helpful :/
	TimeoutError: (req, reply) => {
		return reply.send({ TimeoutError: 404 })
	}
})

fastify.register(function (instance, opts, next) {
	instance.route({
		method: 'GET',
		url: '/*',
		schema: {
			querystring: {
				error: { type: 'boolean' },
				delay: { type: 'number' }
			}

		},
		preHandler: instance.circuitBreaker(),
		handler: async function (req, reply) {
			setTimeout(() => {
				reply.send(
					req.query.error ? { error: 404 } : { hello: 'world' }
				)
			}, req.query.delay || 0)
		}
	})
	next()
})

fastify.listen(3000, err => {
	if (err) throw err
	console.log('Server listening at http://localhost:3000')
})

the error i can't catch him at Object.onSend (\node_modules\fastify-circuit-breaker\index.js:74:19)
return next(new TimeoutError())

:/

What would you like to do with that error?

What would you like to do with that error?

i need catch the error from [setErrorHandler] and then make a Custom response.

Untitled

===================================================

i find this solution
replace return next(new TimeoutError()) to return next(null, JSON.stringify({Error:"timeout"}))
in [\node_modules\fastify-circuit-breaker\index.js:74:19]
and its work but i don't know if this perfect way.

.
.
.
.
.
.
.

i am sorry for my language i know little english and google translate help me ^_^

I think this could be a feature to add: need to add an option onTimeout that must return the body to sent. This is need to maintain the circuit breaker patter

This feature is like this one fastify/fastify-compress#88 if you would like to give it a shot! 💪🏻

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

think this could be a feature to add: need to add an option onTimeout that must return the body to sent. This is need to maintain the circuit breaker patter

Hi - I came across this issue and would like to contribute this feature. In my application, I want to respond with a 504 on a TimeoutError. Having both onTimeout and onCircuitOpen would probably make sense.

An alternative would be to add status code options for both timeoutError and circuitOpenError. Thoughts?