Giter VIP home page Giter VIP logo

error-message's Introduction

@cypress/error-message

User-friendly error text with additional information

NPM

Build status semantic-release js-standard-style

Install

Requires Node version 0.12 or above.

npm install --save @cypress/error-message

Load the function from the module

const {formErrorText} = require('@cypress/error-message')

Use

Think about all possible errors your application might encounter. Describe each one and suggest a solution the user should try in case this particular error happens. Then form the full message once the exception is caught and show it to the user

const fileSaveError = {
  description: 'We could not save an important file',
  solution: `Please check folder permissions and try again

    more details on our FAQ page: https://faq.company.name
  `
}
fs.writeFile(name)
  .catch(
    formErrorText(info).then(console.error)
  )
/*
  shows nice error message

  ------
  We could not save an important file
  Please check folder permissions and try again

    more details on our FAQ page: https://faq.company.name

  Exception message
  ------
  Platform: darwin
  Version: 15.6.2
*/

If you want to include the exception stack, pass printStack option in the info object. For example, a catch-all function should probably print stack to give you a good idea where the problem happens.

const badError = {
  description: 'Unexpected error happened',
  solution: `Something terrible went wrong. Search issues on our
    GitHub repo to find possible solution: https://github.com/company/repo`,
  printStack: true
}
doMyStuff()
  .catch(
    formErrorText(badError).then(console.error)
  )

If info argument is missing a description or a solution, an error will be thrown.

Re-throwing a detailed error

You might not want to handle the error, but rather add details and rethrow it. For this there is a helper function

const {throwDetailedError} = require('@cypress/error-message')
const info = {
  description: 'error description',
  solution: 'our solution'
}
foo()
  .catch(throwDetailedError(info))
  // later catch this detailed error
  .catch(err => {
    // err has original exception + description and solution
    console.error(err)
  })

Examples

Small print

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2017 Cypress.io https://cypress.io

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

error-message's People

Contributors

bahmutov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.