Skip to content

Commit

Permalink
Fix issue with RelayCompilerUserError
Browse files Browse the repository at this point in the history
Reviewed By: kassens

Differential Revision: D5198312

fbshipit-source-id: e9086164c5648ef8870f426d0651986838e169e8
  • Loading branch information
adanoff authored and facebook-github-bot committed Jun 7, 2017
1 parent bd88f74 commit aa78c84
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
3 changes: 1 addition & 2 deletions packages/relay-compiler/codegen/RelayCodegenRunner.js
Expand Up @@ -14,7 +14,6 @@
'use strict';

const RelayCodegenWatcher = require('RelayCodegenWatcher');
const RelayCompilerUserError = require('RelayCompilerUserError');

const invariant = require('invariant');

Expand Down Expand Up @@ -185,7 +184,7 @@ class RelayCodegenRunner {
try {
outputDirectories = await writer.writeAll();
} catch (e) {
if (e instanceof RelayCompilerUserError) {
if (e.isRelayUserError) {
// TODO could use chalk here for red output
console.log('Error: ' + e.message);
} else {
Expand Down
12 changes: 7 additions & 5 deletions packages/relay-compiler/core/RelayCompilerContext.js
Expand Up @@ -13,11 +13,11 @@

'use strict';

const RelayCompilerUserError = require('RelayCompilerUserError');

const immutable = require('immutable');
const invariant = require('invariant');

const {createUserError} = require('RelayCompilerUserError');

import type {Fragment, Root} from 'RelayIR';
import type {GraphQLSchema} from 'graphql';

Expand Down Expand Up @@ -108,9 +108,11 @@ class RelayCompilerContext {
const node = record && record.get('node');
if (!(node && node.kind === 'Fragment')) {
const childModule = name.substring(0, name.lastIndexOf('_'));
throw new RelayCompilerUserError(
`Relay cannot find fragment \`${name}\`.` +
` Please make sure the fragment exists in \`${childModule}\``,
throw createUserError(
'Relay cannot find fragment `%s`.' +
' Please make sure the fragment exists in `%s`',
name,
childModule,
);
}
return node;
Expand Down
14 changes: 8 additions & 6 deletions packages/relay-compiler/core/RelayCompilerUserError.js
Expand Up @@ -13,10 +13,12 @@

'use strict';

class RelayCompilerUserError extends Error {
constructor(message: string) {
super(message);
}
}
const createUserError = (format: string, ...args: any): Error => {
let index = 0;
const formatted = format.replace(/%s/g, match => args[index++]);
const err = new Error(formatted);
(err: any).isRelayUserError = true;
return err;
};

module.exports = RelayCompilerUserError;
module.exports = {createUserError};

0 comments on commit aa78c84

Please sign in to comment.