Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
FeathersJS Error Handler Unexpected Token < Issue

I am working on an Express App with MongoDB and trying to utilize FeathersJS for all my services. Here I'm running a test try to get an error message from the server to the client, but I have an issue with the response from the error handler. My req headers have the correct application/json stuff, so I assumed the Error Handler should send valid json back.

I know I'm not using the next callback in my function, but when I try to do that it gives the same error, so I'm thinking it has to do with the Error Handler. Any direction here would be greatly appreciated!

The first error log is on the server, which is correct.

Bucket Services
error >>>>> Bucket validation failed
Possibly Unhandled Rejection: Bucket validation failed,  Promise { <rejected> 'Bucket validation failed' }
>>>>>> Error: Unexpected token < in JSON at position 0
    at convert (/Users/jaruesink/Documents/Projects/Buckets/node_modules/feathers-rest/node_modules/feathers-errors/lib/index.js:365:79)
    at toError (/Users/jaruesink/Documents/Projects/Buckets/node_modules/feathers-rest/lib/client/base.js:24:37)
    at process._tickCallback (internal/process/next_tick.js:103:7)

my create function within the BucketService class:

  create({
    amount,
    isFund = false,
    name,
    type,
    userID: owner
  }, params, next) {
    const new_bucket = new Bucket({ name, amount, type, isFund, owner });
    return new_bucket.save((error) => {
      console.log('error >>>>>', error.message);
      if (error) { return Promise.reject(error.message); }
      return Promise.resolve(new_bucket);
    });
  }

my router file:

const feathers = require('feathers');
const errorHandler = require('feathers-errors/handler');
const rest = require('feathers-rest');
const router = feathers();

const LoginService = require('../services/login_service');
const UserService = require('../services/user_service');
const BucketService = require('../services/bucket_service');

// Enable REST services
router.configure(rest());

router.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});

router.use('/login', new LoginService());
router.use('/user', new UserService());
router.use('/bucket', new BucketService());

// Set up error handling
router.use(errorHandler());

module.exports = router;
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I figured it out, the key was to correctly pass through a callback (next) function as the third parameter to handle errors. FeathersJS handles the Promise Rejections for you on errors. Then in my test I needed to convert the Feathers-Error to JSON before I could get the message.

I changed my test to:

  it('can validate an incorrect bucket', (done) => {
    const invalid_bucket = {
      name: 'Invalid Bucket',
    };
    bucket_service.create(invalid_bucket, {}, (error) => {
      error = error.toJSON();
      assert(error.message.length > 0);
      done();
    });
  });

and my create function to:

  create({
    amount,
    isFund = false,
    name,
    type,
    userID: owner
  }, params, next) {
    const new_bucket = new Bucket({ name, amount, type, isFund, owner });
    return new_bucket.save()
      .then(created_bucket => Promise.resolve(created_bucket))
      .catch(next);
  }
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda