elierotenberg / http-exceptions

HTTP Exceptions with err codes.
2 stars 0 forks source link

wish: basic documentation #1

Open markstos opened 9 years ago

markstos commented 9 years ago

Hello,

Is this like HTTP::Exception from Perl? It'd be great if the README could be updated with documentation of what methods are contained in here and what an example usage looks like.

elierotenberg commented 9 years ago

Yes, its basically the same idea: using typecheckable/streamlined js errors for the very common use case of HTTP errors.

I will try and document it asap, but you can already see its usage for example here https://github.com/elierotenberg/nexus-uplink-simple-server/blob/master/src/UplinkSimpleServer.js#L155.

Feel free to PR :)

markstos commented 7 years ago

I'm noting this the is other half of the code example: Shown how an exception is caught in a web framework middleware and converted to an HTTP response:

      if (err instanceof HTTPExceptions.HTTPError) {
        return HTTPExceptions.forward(res, err);
      }

Ref: https://github.com/elierotenberg/nexus-uplink-simple-server/blob/d80480bdcd04d5c85e8f1256d96519006372e7bf/dist/UplinkSimpleServer.js#L254

And here are direct examples of what using the module looks like:

       throw new HTTPExceptions.BadRequest(`Missing required field: 'params'.`);
       throw new HTTPExceptions.NotFound(req.path);

I think this is clean way to break the control flow in a promise chain. It's also way way for the "Model" code to communicate with a web framework, without requiring passing around a "request" object or some other hard dependency on a web framework.