stubailo / meteor-rest

:sleeping: simple:rest - make your Meteor app accessible over HTTP and DDP alike
https://atmospherejs.com/simple/rest
MIT License
382 stars 85 forks source link

simple:rest-json-error-handler not work? #122

Closed weilitao closed 8 years ago

weilitao commented 8 years ago

Hello, I have add rest-json-error-handler as example show. What I expect is JSON formatted output, but the actual result is error track:

$ curl -i -H "Accept: application/json" http://localhost:3000/handle-error
HTTP/1.1 404 Not Found
cache-control: no-store
pragma: no-cache
x-content-type-options: nosniff
content-type: text/html; charset=utf-8
content-length: 138
vary: Accept-Encoding
date: Fri, 07 Oct 2016 13:17:00 GMT
connection: keep-alive

Error: Not Found [not-found]<br> &nbsp; &nbsp;at server/main.js:11:15<br> &nbsp; &nbsp;at packages/simple_json-routes/json-routes.js:76:1

Here is my code:

import { Meteor } from 'meteor/meteor';
import { JsonRoutes, RestMiddleware } from 'meteor/simple:json-routes';

Meteor.startup(() => {
  // code to run on server at startup
  JsonRoutes.ErrorMiddleware.use(
    '/handle-error',
    RestMiddleware.handleErrorAsJson
  );
  JsonRoutes.add('get', 'handle-error', function () {
    var error = new Meteor.Error('not-found', 'Not Found');
    error.statusCode = 404;
    throw error;
  });
});

And the environment:

$ meteor --version; node --version; npm --version
Meteor 1.4.1.2
v6.7.0
3.10.8
$ meteor list
blaze-html-templates            1.0.5  Compile HTML templates into reactive UI...
ecmascript                      0.5.8_1  Compiler plugin that supports ES2015+...
es5-shim                        4.6.14_1  Shims and polyfills to improve ECMAS...
jquery                          1.11.9  Manipulate the DOM using CSS selectors
meteor-base                     1.0.4  Packages that every Meteor app needs
mobile-experience               1.0.4  Packages for a great mobile user experi...
mongo                           1.1.12_1  Adaptor for using MongoDB and Minimo...
reactive-var                    1.0.10  Reactive variable
shell-server                    0.2.1  Server-side component of the `meteor sh...
simple:json-routes              2.1.0  The simplest way to define server-side ...
simple:rest                     1.1.1  The easiest way to add a REST API to yo...
simple:rest-json-error-handler  1.0.1  middleware for handling standard Connec...
standard-minifier-css           1.2.1  Standard css minifier used with Meteor ...
standard-minifier-js            1.2.0_1  Standard javascript minifiers used wi...
tracker                         1.1.0  Dependency tracker to allow reactive ca...

So is there some wrong with my use of rest-json-error-handler?

weilitao commented 8 years ago

Sorry, I have found if I move the code out of Meteor.startup, everything works.