├── .flowconfig ├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── yarn.lock /.flowconfig: -------------------------------------------------------------------------------- 1 | [ignore] 2 | 3 | [include] 4 | 5 | [libs] 6 | 7 | [options] 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017-present James Kyle 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-errors 2 | 3 | > Nicer error messages for Babel 4 | 5 | ```js 6 | import createBabelFile from 'babel-file'; 7 | import {BabelError, prettyError, buildCodeFrameError} from 'babel-errors'; 8 | 9 | const file = createBabelFile(...); 10 | 11 | let path = file.path; 12 | let {line, column} = path.loc.start; 13 | 14 | throw prettyError(createErrorWithLoc('Error at this position', line, column)); 15 | throw buildCodeFrameError(path, 'Error with this Path'); 16 | ``` 17 | 18 | ### `createErrorWithLoc(message, line, column)` 19 | 20 | This lets you add location information to your errors. 21 | 22 | ### `wrapErrorWithCodeFrame(err)` 23 | 24 | You can use this when capturing errors thrown by Babel or constructed using 25 | `createErrorWithLoc` to add a code frame when possible. 26 | 27 | ### `buildCodeFrameError(path, message)` 28 | 29 | You can use this to build a code frame error around a path with a specified 30 | message. 31 | 32 | Use this instead of `path.buildCodeFrameError` when you aren't running inside a 33 | Babel plugin. 34 | 35 | ### `toErrorStack(error)` 36 | 37 | Creates string with error name, message, stack/code frame. 38 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // @flow 2 | 'use strict'; 3 | 4 | /*:: 5 | type File = { 6 | code: string, 7 | wrap: (string, Function) => mixed, 8 | }; 9 | 10 | type Path = { 11 | hub: { file: File }, 12 | buildCodeFrameError: string => Error, 13 | }; 14 | */ 15 | 16 | function captureStackTrace(err, fn) { 17 | if (Error.captureStackTrace) { 18 | Error.captureStackTrace(err, fn); 19 | } 20 | } 21 | 22 | function createErrorWithLoc( 23 | message /*: string */, 24 | line /*: number */, 25 | column /*: number */ 26 | ) /*: Error */ { 27 | let err /*: any */ = new SyntaxError(message); 28 | err.loc = {line, column}; 29 | return err; 30 | } 31 | 32 | function wrapErrorWithCodeFrame( 33 | file /*: File */, 34 | error /*: Error */ 35 | ) /*: Error */ { 36 | try { 37 | file.wrap(file.code, () => { 38 | throw error; 39 | }); 40 | } catch (err) { 41 | captureStackTrace(err, wrapErrorWithCodeFrame); 42 | return err; 43 | } 44 | 45 | return error; 46 | } 47 | 48 | function buildCodeFrameError( 49 | path /*: Path */, 50 | message /*: string */ 51 | ) /*: Error */ { 52 | let file = path.hub.file; 53 | let error; 54 | 55 | try { 56 | file.wrap(file.code, () => { 57 | throw path.buildCodeFrameError(message); 58 | }); 59 | error = new Error(message); 60 | } catch (err) { 61 | error = err; 62 | } 63 | 64 | captureStackTrace(error, buildCodeFrameError); 65 | return error; 66 | } 67 | 68 | function toErrorStack(err /*: Error */) { 69 | if (err._babel && err instanceof SyntaxError) { 70 | let stack = `${err.name}: ${err.message}`; 71 | if (err.codeFrame) stack += '\n' + String(err.codeFrame); 72 | return stack; 73 | } else { 74 | return err.stack; 75 | } 76 | } 77 | 78 | module.exports = { 79 | createErrorWithLoc, 80 | wrapErrorWithCodeFrame, 81 | buildCodeFrameError, 82 | toErrorStack, 83 | }; 84 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-errors", 3 | "version": "1.1.1", 4 | "description": "Nicer error messages for Babel", 5 | "main": "index.js", 6 | "author": "James Kyle ", 7 | "license": "MIT", 8 | "files": [], 9 | "devDependencies": { 10 | "flow-bin": "^0.46.0" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | flow-bin@^0.46.0: 6 | version "0.46.0" 7 | resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.46.0.tgz#06ad7fe19dddb1042264438064a2a32fee12b872" 8 | --------------------------------------------------------------------------------