@@ -33,8 +33,8 @@ export interface initParams {
33
33
routes : Array < Route > ,
34
34
hooks : any ,
35
35
history ?: any ,
36
- silent ?: boolean ,
37
- ctx : any
36
+ ctx : any ,
37
+ errors : any
38
38
}
39
39
export interface initResult {
40
40
Router ?: any ,
@@ -48,10 +48,11 @@ export interface initResult {
48
48
49
49
export default class Router extends React . Component < Props , State > {
50
50
router : any ;
51
+ errors : any ;
51
52
path : string ;
52
53
location : any ;
53
54
private subscriber : any ;
54
- constructor ( { Component, componentProps, router, path, location } ) {
55
+ constructor ( { Component, componentProps, router, path, location, errors } ) {
55
56
super ( ) ;
56
57
this . state = {
57
58
Component,
@@ -61,18 +62,22 @@ export default class Router extends React.Component<Props, State> {
61
62
} ;
62
63
63
64
this . router = router ;
65
+ this . errors = errors ;
64
66
this . subscriber = null ;
65
67
}
66
68
static async init ( opts : initParams ) : Promise < initResult > {
67
- const { path, routes, hooks, history = null , silent = false , ctx = new Context ( ) } = opts ;
69
+ const { path, routes, hooks, history = null , ctx = new Context ( ) , errors } = opts ;
68
70
let plainRoutes ;
69
71
if ( ( Array . isArray ( routes ) && React . isValidElement ( routes [ 0 ] ) ) || React . isValidElement ( routes ) ) {
70
72
plainRoutes = Router . buildRoutes ( routes ) ;
71
73
} else {
72
74
plainRoutes = routes ;
73
75
}
74
76
const router = new RouterAsync ( { routes : plainRoutes , hooks } ) ;
75
- const { location, route, status, params, redirect, result } = await router . run ( { path, ctx, silent } ) ;
77
+ let { location, route, status, params, redirect, result, error } = await router . run ( { path, ctx } ) ;
78
+ if ( error !== null ) {
79
+ result = Router . getErrorComponent ( error , errors ) ;
80
+ }
76
81
const componentProps = {
77
82
router : {
78
83
path,
@@ -81,7 +86,8 @@ export default class Router extends React.Component<Props, State> {
81
86
status,
82
87
params,
83
88
redirect,
84
- ctx
89
+ ctx,
90
+ error
85
91
}
86
92
} ;
87
93
@@ -95,10 +101,12 @@ export default class Router extends React.Component<Props, State> {
95
101
router,
96
102
path,
97
103
location,
98
- history
104
+ history,
105
+ errors
99
106
} ,
100
107
componentProps,
101
- callback : this . makeCallback ( router , { path, location, route, status, params, redirect, result, ctx } )
108
+ callback : this . makeCallback ( router , { path, location, route, status, params, redirect, result, ctx } ) ,
109
+ error
102
110
}
103
111
}
104
112
static buildRoutes ( routes ) {
@@ -124,10 +132,22 @@ export default class Router extends React.Component<Props, State> {
124
132
subscribe ( callback : Function ) {
125
133
this . subscriber = callback . bind ( this ) ;
126
134
}
127
- changeComponent ( { Component, componentProps, path, location, renderCallback } ) {
135
+ static getErrorComponent ( error , errors ) {
136
+ if ( error . status in errors ) {
137
+ return errors [ error . status ] ;
138
+ }
139
+ if ( '*' in errors ) {
140
+ return errors [ '*' ] ;
141
+ }
142
+ return 'Internal error' ;
143
+ }
144
+ changeComponent ( { Component, componentProps, path, location, error, renderCallback } ) {
128
145
if ( this . subscriber ) {
129
- this . subscriber ( { Component, componentProps, path, location, renderCallback } ) ;
146
+ this . subscriber ( { Component, componentProps, path, location, error , renderCallback } ) ;
130
147
} else {
148
+ if ( error !== null ) {
149
+ Component = Router . getErrorComponent ( error , this . errors ) ;
150
+ }
131
151
this . setState ( {
132
152
path,
133
153
location,
@@ -136,16 +156,6 @@ export default class Router extends React.Component<Props, State> {
136
156
} , renderCallback ) ;
137
157
}
138
158
}
139
- replaceComponent ( Component , componentProps ) {
140
- if ( this . subscriber ) {
141
- this . subscriber ( { Component, componentProps, path : this . state . path , location : this . state . location } )
142
- } else {
143
- this . setState ( {
144
- Component,
145
- componentProps
146
- } ) ;
147
- }
148
- }
149
159
getState ( ) {
150
160
return this . state ;
151
161
}
0 commit comments