2017-01-26 17:07:37 -08:00
/ * *
* @license
* Copyright Google Inc . All Rights Reserved .
*
* Use of this source code is governed by an MIT - style license that can be
* found in the LICENSE file at https : //angular.io/license
* /
2017-03-14 09:16:15 -07:00
import { ERROR_DEBUG_CONTEXT , ERROR_LOGGER , getDebugContext } from '../errors' ;
2017-02-03 15:20:50 -08:00
import { DebugContext , ViewState } from './types' ;
2017-01-26 17:07:37 -08:00
export function expressionChangedAfterItHasBeenCheckedError (
2017-01-27 13:19:00 -08:00
context : DebugContext , oldValue : any , currValue : any , isFirstCheck : boolean ) : Error {
2017-01-26 17:07:37 -08:00
let msg =
2017-02-03 15:20:50 -08:00
` ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: ' ${ oldValue } '. Current value: ' ${ currValue } '. ` ;
2017-01-26 17:07:37 -08:00
if ( isFirstCheck ) {
msg +=
` It seems like the view has been created after its parent and its children have been dirty checked. ` +
` Has it been created in a change detection hook ? ` ;
}
2017-01-31 14:52:01 -08:00
return viewDebugError ( msg , context ) ;
2017-01-26 17:07:37 -08:00
}
2017-03-14 09:16:15 -07:00
export function viewWrappedDebugError ( err : any , context : DebugContext ) : Error {
if ( ! ( err instanceof Error ) ) {
// errors that are not Error instances don't have a stack,
// so it is ok to wrap them into a new Error object...
err = new Error ( err . toString ( ) ) ;
}
_addDebugContext ( err , context ) ;
2017-01-26 17:07:37 -08:00
return err ;
}
2017-01-27 13:19:00 -08:00
export function viewDebugError ( msg : string , context : DebugContext ) : Error {
const err = new Error ( msg ) ;
2017-03-14 09:16:15 -07:00
_addDebugContext ( err , context ) ;
2017-01-26 17:07:37 -08:00
return err ;
}
2017-03-14 09:16:15 -07:00
function _addDebugContext ( err : Error , context : DebugContext ) {
( err as any ) [ ERROR_DEBUG_CONTEXT ] = context ;
( err as any ) [ ERROR_LOGGER ] = context . logError . bind ( context ) ;
}
2017-01-27 13:19:00 -08:00
export function isViewDebugError ( err : Error ) : boolean {
return ! ! getDebugContext ( err ) ;
2017-01-26 17:07:37 -08:00
}
2017-01-31 14:52:01 -08:00
2017-02-03 15:20:50 -08:00
export function viewDestroyedError ( action : string ) : Error {
return new Error ( ` ViewDestroyedError: Attempt to use a destroyed view: ${ action } ` ) ;
2017-01-31 14:52:01 -08:00
}