@@ -33,8 +33,8 @@ export interface initParams {
3333 routes : Array < Route > ,
3434 hooks : any ,
3535 history ?: any ,
36- silent ?: boolean ,
37- ctx : any
36+ ctx : any ,
37+ errors : any
3838}
3939export interface initResult {
4040 Router ?: any ,
@@ -48,10 +48,11 @@ export interface initResult {
4848
4949export default class Router extends React . Component < Props , State > {
5050 router : any ;
51+ errors : any ;
5152 path : string ;
5253 location : any ;
5354 private subscriber : any ;
54- constructor ( { Component, componentProps, router, path, location } ) {
55+ constructor ( { Component, componentProps, router, path, location, errors } ) {
5556 super ( ) ;
5657 this . state = {
5758 Component,
@@ -61,18 +62,22 @@ export default class Router extends React.Component<Props, State> {
6162 } ;
6263
6364 this . router = router ;
65+ this . errors = errors ;
6466 this . subscriber = null ;
6567 }
6668 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 ;
6870 let plainRoutes ;
6971 if ( ( Array . isArray ( routes ) && React . isValidElement ( routes [ 0 ] ) ) || React . isValidElement ( routes ) ) {
7072 plainRoutes = Router . buildRoutes ( routes ) ;
7173 } else {
7274 plainRoutes = routes ;
7375 }
7476 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+ }
7681 const componentProps = {
7782 router : {
7883 path,
@@ -81,7 +86,8 @@ export default class Router extends React.Component<Props, State> {
8186 status,
8287 params,
8388 redirect,
84- ctx
89+ ctx,
90+ error
8591 }
8692 } ;
8793
@@ -95,10 +101,12 @@ export default class Router extends React.Component<Props, State> {
95101 router,
96102 path,
97103 location,
98- history
104+ history,
105+ errors
99106 } ,
100107 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
102110 }
103111 }
104112 static buildRoutes ( routes ) {
@@ -124,10 +132,22 @@ export default class Router extends React.Component<Props, State> {
124132 subscribe ( callback : Function ) {
125133 this . subscriber = callback . bind ( this ) ;
126134 }
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 } ) {
128145 if ( this . subscriber ) {
129- this . subscriber ( { Component, componentProps, path, location, renderCallback } ) ;
146+ this . subscriber ( { Component, componentProps, path, location, error , renderCallback } ) ;
130147 } else {
148+ if ( error !== null ) {
149+ Component = Router . getErrorComponent ( error , this . errors ) ;
150+ }
131151 this . setState ( {
132152 path,
133153 location,
@@ -136,16 +156,6 @@ export default class Router extends React.Component<Props, State> {
136156 } , renderCallback ) ;
137157 }
138158 }
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- }
149159 getState ( ) {
150160 return this . state ;
151161 }
0 commit comments