npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

throw-hydra

v1.0.2

Published

A full Hydra REST API error collection

Downloads

4

Readme

throw-hydra

HTTP Status Code Handler

An HTTP Error collection to use in your next Hydra REST API.

This is a JSON-LD/Hydra adaptation of https://github.com/kbariotis/throw.js

Installation

npm install throw-hydra.js

Example

var express = require('express');
var app = express();
var throwjs = require('throw.js');
var logger = require('winston');

app.get('/', function (req, res, next) {
  
  next(new throwjs.notFound());
  
});

app.get('/hi', function (req, res, next) {
  
  var options = {
      title: "Oops, nothing to see here.",
      description: "Try another day?",
      next: "/users/",
      prev: req.url         // express can generate the source url for us.
  }
  
  next(new throwjs.notFound(options));
  
});

app.use(function(err, req, res, next) {

    /* 
     The code block below is good-practice for production environment to avoid error traces being revealed to clients, but optional.
        
    if (req.app.get('env') !== 'development' &&
        req.app.get('env') !== 'test') {

        delete err.stack;
    }
    */

    // We catch the error generated by the middleware in this block, we reference err.statusCode[0] as it contains the default RFC compliant error code and is also the default in the absence of a user supplied one.
    res.status(err.statusCode[0] || 500).json(err);

});

var server = app.listen(3000, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});

Custom Errors


throwjs.customError(options);

Optional Parameters:

  • title[optional]: Title error. Defaults to RFC7231 title if non supplied.
  • statusCode[optional]: The HTTP Status number to return, multiple status Codes can be provided in addition to the default one. This allows to augment standard HTTP Status Codes (e.g. 403, with a custom, application-specific status code).
  • description[optional]: A detailed description of the error to provide the user with more context.
  • type[optional]: A JSON-LD/hydra property allowing you to specify the type of this status code (e.g. type = "Error"). See Hydra's documentaiton for more information.
  • next[optional]: The next logical place for the user to navigate to. See hydra:next
  • previous[optional]: URL leading to this error. See hydra:previous

An example of the options in action could be:


     next(new throwjs.notFound({
      
      title: "Nothing to see here."
      description: "Oops, nothing is at this location, try again later?",
      type: "Error",
      next: "/api/users/",
      previous: "/api/users/non-existant-user"  
         
     }));

Errors

All of the classes below have all parameters set up by default, based on RFC7231. But you can override the message and the errorCode to fit your for personal needs.

400 Bad Request

throwjs.badRequest(options);

401 Unauthorized

throwjs.unauthorized(options);

402 Payment Required

throwjs.paymentRequired(options);

403 Forbidden

throwjs.forbidden(options);

404 Not Found

throwjs.notFound(options);

405 Method Not Allowed

throwjs.methodNotAllowed(options);

406 Not Acceptable

throwjs.notAcceptable(options);

407 Proxy Authentication Required

throwjs.proxyAuthenticationRequired(options);

408 Request Timeout

throwjs.requestTimeout(options);

409 Conflict

throwjs.conflict(options);

410 Gone

throwjs.gone(options);

422 Unprocessable Entity

throwjs.unprocessableEntity(options);

424 Failed Dependency

throwjs.failedDependency(options);

500 Internal Server Error

throwjs.internalServerError(options);

501 Not Implemented

throwjs.notImplemented(options);

502 Bad Gateway

throwjs.badGateway(options);

503 Service Unavailable

throwjs.serviceUnavailable(options);

504 Gateway Timeout

throwjs.gatewayTimeout(options);

505 HTTP Version Not Supported

throwjs.httpVersionNotSupported(options);

511 Network Authentication Required

throwjs.networkAuthenticationRequired(options);

TODO

  • Implement more Error Classes