Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
fix(errors): handle errors that aren't syntax errors
Browse files Browse the repository at this point in the history
Previously, we were assuming all errors thrown during execution of the formatters were going to be
syntax errors. However, this is not the case, so we must handle these errors elegantly by displaying
them in a popup.

Resolves #231
  • Loading branch information
robwise committed Jul 22, 2017
1 parent d3a9456 commit c3d02b0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions decls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ declare type Linter$Message$ApplySolution = {
declare type Prettier$SyntaxError = {
loc: { start: { line: number, column: number } } | {| line: number, column: number |},
message: string,
stack: string,
};
declare type Linter$Message = {
// NOTE: These are given by providers
Expand Down
20 changes: 16 additions & 4 deletions dist/executePrettier/handleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ var _require = require('../editorInterface'),

var linter = require('../linterInterface');

var _require2 = require('../helpers'),
createPoint = _require2.createPoint,
createRange = _require2.createRange;
var _require2 = require('../atomInterface'),
addErrorNotification = _require2.addErrorNotification;

var _require3 = require('../helpers'),
createPoint = _require3.createPoint,
createRange = _require3.createRange;

var errorLine = function errorLine(error) {
return error.loc.start ? error.loc.start.line : error.loc.line;
Expand Down Expand Up @@ -44,6 +47,15 @@ var setErrorMessageInLinter = function setErrorMessageInLinter(_ref) {
}]);
};

var handleError = _.flow(setErrorMessageInLinter, _.stubFalse);
var isSyntaxError = _.overSome([_.flow(_.get('error.loc.start.line'), _.isInteger), _.flow(_.get('error.loc.line'), _.isInteger)]);

var displayErrorInPopup = function displayErrorInPopup(args) {
return addErrorNotification('prettier-atom failed: ' + args.error.message, {
stack: args.error.stack,
dismissable: true
});
};

var handleError = _.cond([[isSyntaxError, setErrorMessageInLinter], [_.stubTrue, displayErrorInPopup]]);

module.exports = handleError;
17 changes: 16 additions & 1 deletion src/executePrettier/handleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const _ = require('lodash/fp');
const { getCurrentFilePath } = require('../editorInterface');
const linter = require('../linterInterface');
const { addErrorNotification } = require('../atomInterface');
const { createPoint, createRange } = require('../helpers');

type HandleErrorArgs = {
Expand Down Expand Up @@ -40,6 +41,20 @@ const setErrorMessageInLinter = ({ editor, bufferRange, error }: HandleErrorArgs
},
]);

const handleError: HandleErrorArgs => false = _.flow(setErrorMessageInLinter, _.stubFalse);
const isSyntaxError: HandleErrorArgs => boolean = _.overSome([
_.flow(_.get('error.loc.start.line'), _.isInteger),
_.flow(_.get('error.loc.line'), _.isInteger),
]);

const displayErrorInPopup = (args: HandleErrorArgs) =>
addErrorNotification(`prettier-atom failed: ${args.error.message}`, {
stack: args.error.stack,
dismissable: true,
});

const handleError: HandleErrorArgs => void = _.cond([
[isSyntaxError, setErrorMessageInLinter],
[_.stubTrue, displayErrorInPopup],
]);

module.exports = handleError;
10 changes: 10 additions & 0 deletions src/executePrettier/handleError.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
jest.mock('../editorInterface');
jest.mock('../linterInterface');
jest.mock('../atomInterface');

const { getCurrentFilePath } = require('../editorInterface');
const linterInterface = require('../linterInterface');
const { addErrorNotification } = require('../atomInterface');
const { createRange } = require('../helpers');
const handleError = require('./handleError');

Expand Down Expand Up @@ -87,3 +89,11 @@ describe('position property of the message sent to the linter', () => {
expect(positionOfFirstCallOfFirstMessageOfSetMessages()).toEqual(expectedPosition);
});
});

it('displays errors in a popup if they are not syntax errors', () => {
const error = new Error('fake error');

handleError({ error });

expect(addErrorNotification).toHaveBeenCalled();
});

0 comments on commit c3d02b0

Please sign in to comment.