Skip to content

BrianARuff/format-errors-pg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Purpose of Application

Use this application to protect people from technical details by abstracting Node + PG error messages.

How to install package

  1. Make sure you are CD'd into the same level of your application's package.json file.

  2. In the CLI run npm i format-errors-pg


How to use this package

  1. Require the function from its file in node modules.

const formatErrorPG = require("format-errors-pg");

  1. Use the function in proper location
router.post("/register", async (req, res) => {

  const { name, password } = req.body;

  const newUser = {
    name,
    password
  }

  try {
    await database.query("INSERT into USERS ( name, password ) VALUES ( $1, $2 )", [
      name,
      password
    ]);
    return res.status(200).json({newUser})
  } catch (error) {
    res.status(500).json({ error: formatPGErrors(error) });
  }
});

Notice this part in the .catch block:

res.status(500).json({ error: formatPGErrors(error) });

Note that the .stack property is somewhat hidden on the error object that is sent back from PG. However, it IS there.

That is where the function should be used, with 500 errors inside of catch blocks.

It works with promises as well if you do not want/can-not use async and await for some reason :).


Example output ( in postman )

Inside of the Postman application I made a HTTP POST request to my application's end-point, and it returned the following output:


{
    "error": "duplicate key value violates unique constraint
    \"users_name_key\"",
}

As you can see the error message is pretty clear. We are dealing with a user's name duplication error.

  • A more experienced BE dev will say we have an error on the table users at the column entitled name, and then fill in the rest of it by the other parts of the message.

More practice!!


{
    "error": "duplicate key value violates unique constraint 
    \"users_email_key\""
}

  • FED sees this and will probably think hmm, we probably have a duplicate email address in use.

  • BED/full-stack/genius-wizard might think error at table users at column email, and then fill in the rest of it by the other parts of the message.


Note

If you are more experienced with Node + PG and the BE in general then you might be thinking "this is already in the error message". I say yes, it IS in the error message. However, this is a message that a front-end developer can easily understand without being scared off by a giant wall of error text. It removes the rest of the message which contains all the file paths and other data that would be irrelevant to a FED that are stored in the error stack.


About

Use this application to protect people from technical details by abstracting Node + PG error messages.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published