Skip to content

Commit 26f6989

Browse files
christopherthielenmergify[bot]
authored andcommitted
fix(errors): Always throw a new Error() so stacktraces are usable
1 parent d5e4469 commit 26f6989

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

src/components/UIRouter.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface UIRouterProps {
3232
}
3333

3434
/** @hidden */
35-
export const InstanceOrPluginsMissingError = new Error(`Router instance or plugins missing.
35+
export const InstanceOrPluginsMissingError = `Router instance or plugins missing.
3636
You must either provide a location plugin via the plugins prop:
3737
3838
<UIRouter plugins={[pushStateLocationPlugin]} states={[···]}>
@@ -47,12 +47,10 @@ export const InstanceOrPluginsMissingError = new Error(`Router instance or plugi
4747
<UIRouter router={router}>
4848
<UIView />
4949
</UIRouter>
50-
`);
50+
`;
5151

5252
/** @hidden */
53-
export const UIRouterInstanceUndefinedError = new Error(
54-
`UIRouter instance is undefined. Did you forget to include the <UIRouter> as root component?`
55-
);
53+
export const UIRouterInstanceUndefinedError = `UIRouter instance is undefined. Did you forget to include the <UIRouter> as root component?`;
5654

5755
/**
5856
* @internalapi
@@ -84,7 +82,7 @@ export function useInitializeUIRouter(
8482
if (configFn) configFn(uiRouter.current);
8583
(states || []).forEach(state => uiRouter.current.stateRegistry.register(state));
8684
} else {
87-
throw InstanceOrPluginsMissingError;
85+
throw new Error(InstanceOrPluginsMissingError);
8886
}
8987

9088
uiRouter.current.start();

src/components/UISref.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import { UISrefActiveContext } from './UISrefActive';
1818
/** @hidden */
1919
let classNames = _classNames;
2020

21+
/** @hidden */
22+
export const IncorrectStateNameTypeError = `State name provided to UISref (as 'to') must be a string.`;
23+
2124
export interface UISrefProps {
2225
children?: any;
2326
to: string;
@@ -48,7 +51,7 @@ export function useUISref(to: string, params: { [key: string]: any } = {}, optio
4851

4952
const { stateService, stateRegistry } = router;
5053
if (!isString(to)) {
51-
throw new Error(`State name provided to UISref (as 'to') must be a string.`);
54+
throw new Error(IncorrectStateNameTypeError);
5255
}
5356

5457
useEffect(() => parentUISrefActiveAddStateInfo(to, params), []);

src/components/UIView.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,9 @@ export interface UIViewState {
9797
props?: any;
9898
}
9999

100-
export const TransitionPropCollisionError = new Error(
100+
export const TransitionPropCollisionError =
101101
'`transition` cannot be used as resolve token. ' +
102-
'Please rename your resolve to avoid conflicts with the router transition.'
103-
);
102+
'Please rename your resolve to avoid conflicts with the router transition.';
104103

105104
/** @internalapi */
106105
export const UIViewContext = createContext<UIViewAddress>(undefined);
@@ -174,7 +173,7 @@ class View extends Component<UIViewProps, UIViewState> {
174173
componentDidMount() {
175174
const router = this.props.router;
176175
if (typeof router === 'undefined') {
177-
throw UIRouterInstanceUndefinedError;
176+
throw new Error(UIRouterInstanceUndefinedError);
178177
}
179178

180179
// Check the context for the parent UIView's fqn and State
@@ -234,7 +233,7 @@ class View extends Component<UIViewProps, UIViewState> {
234233

235234
let stringTokens: string[] = resolveContext.getTokens().filter(x => typeof x === 'string');
236235
if (stringTokens.indexOf('transition') !== -1) {
237-
throw TransitionPropCollisionError;
236+
throw new Error(TransitionPropCollisionError);
238237
}
239238

240239
trans = injector.get(Transition);

src/components/hooks/useUIRouter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useContext } from 'react';
2-
import { UIRouterContext } from '../UIRouter';
2+
import { UIRouterContext, UIRouterInstanceUndefinedError } from '../UIRouter';
33

44
/** Returns the UIRouter object from React Context */
55
export function useUIRouter() {
66
const router = useContext(UIRouterContext);
77
if (!router) {
8-
throw new Error(`UIRouter instance is undefined. Did you forget to include the <UIRouter> as root component?`);
8+
throw new Error(UIRouterInstanceUndefinedError);
99
}
1010
return router;
1111
}

src/core.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import { ReactViewConfig, reactViewsBuilder } from './reactViews';
2222
let viewConfigFactory = (node: [PathNode], config: ReactViewDeclaration) => new ReactViewConfig(node, config);
2323

2424
/** @hidden */
25-
export const StartMethodCalledMoreThanOnceError = new Error(`
25+
export const StartMethodCalledMoreThanOnceError = `
2626
The Router.start() method has been called more than once.
2727
2828
The <UIRouter> component calls start() as final step of the initialization and you shouldn't need to call it manually.
29-
`);
29+
`;
3030

3131
/**
3232
* The main UIRouter object
@@ -65,7 +65,7 @@ export class UIRouterReact extends UIRouter {
6565
start(): void {
6666
// Throw error if user calls `start` more than once
6767
if (this.started) {
68-
throw StartMethodCalledMoreThanOnceError;
68+
throw new Error(StartMethodCalledMoreThanOnceError);
6969
} else {
7070
this.urlMatcherFactory.$get();
7171
this.urlRouter.listen();

0 commit comments

Comments
 (0)