question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question: HttpError returned as string instead of JSON

See original GitHub issue

System:

node: v12.9.1 npm: 6.10.2 typescript: 3.7.5

index.ts

import 'reflect-metadata';
import { createExpressServer } from 'routing-controllers';

const app = createExpressServer({
  controllers: [__dirname + '/**/*.controller.js'], // even if reflection isn't used and the Controller is imported directly it doesn't work
});
app.listen(3000);

example/example.controller.ts

import { Controller, NotFoundError, Get } from "routing-controllers";

@Controller('/example')
export class ExampleController {
  @Get()
  public get(): string {
    throw new NotFoundError('what!');
  }
}

Causes the following error to be produced

Error at NotFoundError.HttpError [as constructor] (/Users/john.doe/Documents/code/routing-controllers/node_modules/routing-controllers/http-error/HttpError.js:30:23) at new NotFoundError (/Users/john.doe/Documents/code/routing-controllers/node_modules/routing-controllers/http-error/NotFoundError.js:23:28) at ExampleController.get (/Users/john.doe/Documents/code/routing-controllers/dist/example/example.controller.js:17:15) at ActionMetadata.callMethod (/Users/john.doe/Documents/code/routing-controllers/node_modules/routing-controllers/metadata/ActionMetadata.js:108:48) at /Users/john.doe/Documents/code/routing-controllers/node_modules/routing-controllers/RoutingControllers.js:99:140 at processTicksAndRejections (internal/process/task_queues.js:85:5)

According to the documentation I should expect

{
  "name": "NotFoundError",
  "message": "what!"
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jotamoraiscommented, May 1, 2020

@dioio-io, @ahmedabdelhafez, @marines, @franckbattu this is expected behavior when NODE_ENV is not production.

(property) RoutingControllersOptions.development?: boolean Indicates if development mode is enabled. By default, it’s enabled if your NODE_ENV is not equal to “production”.

To test locally, you can set the development property of RoutingControllersOptions, to ‘false’.

import "reflect-metadata";
import { createExpressServer } from "routing-controllers";

createExpressServer({
  development: false,
  controllers: [__dirname + "/**/*.controller.ts"] // even if reflection isn't used and the Controller is imported directly it doesn't work
}).listen(3000);

I’ve created this sandbox to show.

Similar: #473, #460, #440,

0reactions
NoNameProvidedcommented, Aug 9, 2020

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular4 - Http error returned is a string instead of an object
I'm looking at a json formatted string in your sample, it's wrong in the sample or on the server. Objects aren't surrounded in...
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
Read more >
HTTP status and error codes for JSON | Cloud Storage
The following document provides reference information about the status codes and error messages that are used in the Cloud Storage JSON API.
Read more >
How to Parse a JSON Request Body in Go - Alex Edwards
A solution which I've found works well is to create a decodeJSONBody() helper function, and have this return a custom malformedRequest error ...
Read more >
Python Parse JSON response using requests library - PYnative
You can get a 204 error In case the JSON decoding fails. The response.json() raises an exception in the following scenario. The response...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found