'fastify-http-proxy: Failure with "Promise may not be fulfilled with 'undefined'
I am using the fastify with fastify-http-proxy with the following configuration:
const proxy = require('fastify-http-proxy');
fastify.register(proxy, {
upstream: "https://myendpoint/occm/api/azure",
prefix: '/azure',
onResponse: (request, reply, res) => {
console.log('Inside onResponse')
reply.send('New body of different length');
},
onError: (reply, error) => {
console.log('Inside onError')
reply.send(`My Error: ${error}`);
},
replyOptions: {
rewriteRequestHeaders: (originalRequest, headers) => {
console.log('Inside rewriteRequestHeaders')
return {
...headers,
};
},
},
});
I have configured a fastify route as this:
fastify.route({
method: 'GET',
url: `/azure/vsa/working-environments`,
schema: {
description: 'Trying out',
tags: ['notifications'],
security: [{
authorization: []
}],
params:{
title: "Working Env GET",
description:'Trying out...',
type: 'object',
example: 'No input',
properties: {
'accountId': {
description: "Account ID",
type: 'string'
},
},
}
},
handler: async (request, reply) => {
}
I am calling it as:
GET http://localhost:xxxx/azure/vsa/working-environments
Basically I would like to proxy the request to this endpoint:
https://myendpoint/occm/api/azure/vsa/working-environments
Now whenever I am running it, getting this error:
{"level":30,"time":1644249539394,"pid":12100,"hostname":"pradipm02-PC","requestId":"Q3T9RjI11N","req":{"method":"GET","url":"/azure/vsa/working-environments","hostname":"localhost:8092","remoteAddress":"127.0.0.1","remotePort":63308},"msg":"incoming request"}
{"level":50,"time":1644249540657,"pid":12100,"hostname":"pradipm02-PC","requestId":"Q3T9RjI11N","err":{"type":"FastifyError","message":"Promise may not be fulfilled with 'undefined' when statusCode is not 204","stack":"FastifyError: Promise may not be fulfilled with 'undefined' when statusCode is not 204\n at C:\\Users\\pradipm\\clients\\CloudManager\\cm_2\\occm\\service-infra\\notifications\\node_modules\\fastify\\lib\\wrapThenable.js:30:30\n at processTicksAndRejections (node:internal/process/task_queues:96:5)","name":"FastifyError","code":"FST_ERR_PROMISE_NOT_FULFILLED","statusCode":500},"msg":"Promise may not be fulfilled with 'undefined' when statusCode is not 204"}
Any of the console.log in the rewriteRequestHeaders, onReponse, onError are not showing up.
Any help is appreciated.
Solution 1:[1]
This is discussed in the following thread in fastify-http-proxy forum. It is suggested to use fastify-reply-from instead. That solves my issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pradip |
