Store: Errors are swallowed in selectors

Created on 15 Feb 2019  路  8Comments  路  Source: ngxs/store

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => https://github.com/ngxs/store/blob/master/CONTRIBUTING.md
[ ] Other... Please describe:

Current behavior


When error occurs in a selector - it is swallowed and not shown in the console.

Expected behavior


Error should be shown in the console.

Minimal reproduction of the problem with instructions

  @Selector() 
  static pandas(state: string[]) {
    const foo: any = null;
    console.log(foo.bar);
    return state.filter(s => s.indexOf('panda') > -1);
  }

What is the motivation / use case for changing the behavior?


Finding bugs easier in selectors.

Environment


Libs:
- @angular/core version: 7.1.4
- @ngxs/store version: 3.3.3


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

For Tooling issues:
- Node version: XX  
- Platform:  

Others:

core

Most helpful comment

@ogix @sroettering You can try a new option suppressErrors.

Will be released in 3.5.0

$ npm install @ngxs/store@dev
@NgModule({
  imports: [
       NgxsModule.forRoot([TasksState], {
            developmentMode: true,
            selectorOptions: { suppressErrors: false } // default: true
       })
  ]
})
export class AppModule {}

All 8 comments

@ogix selectors calls are wrapped in try-catch, this decision was made in order to avoid exceptions. E.g. you've got some state that stores a null value or some object, and you want to create a selector that returns token value

@State<Token | null>({
  name: 'token',
  defaults: null
})
class TokenState {
  @Selector()
  public static getAccessToken(state: Token) {
    return state.accessToken;
  }
}

this won't throw and will return undefined.

@splincode what do you think?

@arturovt A console.log could be added to the catch, if developmentMode is turned on.

console.error perhaps instead when dev mode?

Isn't it possible to check if state is null before applying a selector function? And then replace try/catch with that check.

The swallowing is pretty annoying for testing, too. Most of the time I have to guess which properties I forgot to add to the initial state setup.

I think at least logging in dev mode should be added.

@ogix @sroettering You can try a new option suppressErrors.

Will be released in 3.5.0

$ npm install @ngxs/store@dev
@NgModule({
  imports: [
       NgxsModule.forRoot([TasksState], {
            developmentMode: true,
            selectorOptions: { suppressErrors: false } // default: true
       })
  ]
})
export class AppModule {}

Thanks!

Released in 3.5.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TomDemulierChevret picture TomDemulierChevret  路  3Comments

splincode picture splincode  路  4Comments

eranshmil picture eranshmil  路  5Comments

piernik picture piernik  路  5Comments

goodmite picture goodmite  路  5Comments