Angular 在何处/如何捕获NGXS操作中发生的异常?
我有一个Ionic应用程序,它使用NGX存储状态 我已经开始实现我的auth服务,它使用firebase behind(与angularfire一起) 所以,基本上,我有这个动作:Angular 在何处/如何捕获NGXS操作中发生的异常?,angular,exception,state-management,ngxs,Angular,Exception,State Management,Ngxs,我有一个Ionic应用程序,它使用NGX存储状态 我已经开始实现我的auth服务,它使用firebase behind(与angularfire一起) 所以,基本上,我有这个动作: @Action(LoginWithPasswordAction) async loginWithPassword(ctx: StateContext<AuthStateModel>, action: LoginWithPasswordAction) { const resul
@Action(LoginWithPasswordAction)
async loginWithPassword(ctx: StateContext<AuthStateModel>, action: LoginWithPasswordAction) {
const result = await this.angularFireAuth.signInWithEmailAndPassword(action.email, action.password);
await this.router.navigate(['/']);
}
当我成功登录时,这项功能可以完美地工作
现在,我必须管理错误案例。目前,如果我使用了错误的密码,firebase会抛出一个异常
我希望能够在我的登录组件中,在我发送操作的地方“捕获”它:
async login() {
if (this.loginForm.valid) {
try {
await this.store.dispatch(
new LoginWithPasswordAction(this.loginForm.get('email').value, this.loginForm.get('password').value)
);
this.navController.navigateRoot('/');
} catch (ex) {
console.log(ex)
this.loginError = ex.message;
}
}
}
但是我没有收到异常,我想是因为NGXS为我捕获了它(在日志中,我看到错误来自NGXS日志插件)
因此:
在NGXS概念中,在哪里/如何捕获/处理操作中发生的异常?我想我找到了答案 根据文档(my bad),显然有两种捕获异常的方法: 在行动中
@Action(HandledError)
handledError(ctx: StateContext<StateModel>) {
try {
// error is thrown
} catch (err) {
console.log('error catched inside @Action wont propagate to ErrorHandler or dispatch subscription')
}
}
@操作(HandleError)
HandleError(ctx:StateContext){
试一试{
//错误被抛出
}捕捉(错误){
console.log(@Action内捕获的错误不会传播到ErrorHandler或分派订阅)
}
}
或者在您发送操作的位置:
@Action(UnhandledError)
unhandledError(ctx: StateContext<StateModel>) {
// error is thrown, DO NOT CATCH IT
}
unhandled() {
this.store.dispatch(new UnhandledError()).pipe(
catchError(err => {
console.log('unhandled error on dispatch subscription')
return of('')
})
).subscribe();
}
@操作(未处理错误)
未处理的错误(ctx:StateContext){
//错误被抛出,不要捕获它
}
未处理的(){
this.store.dispatch(新的未处理错误()).pipe(
catchError(err=>{
console.log('分派订阅上未处理的错误')
返回(“”)
})
).subscribe();
}
显然,它更喜欢捕获操作中的异常,并用它更新状态
@Action(UnhandledError)
unhandledError(ctx: StateContext<StateModel>) {
// error is thrown, DO NOT CATCH IT
}
unhandled() {
this.store.dispatch(new UnhandledError()).pipe(
catchError(err => {
console.log('unhandled error on dispatch subscription')
return of('')
})
).subscribe();
}