Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw error when before middleware returns undefined #49

Open
capaj opened this issue Jan 13, 2017 · 9 comments
Open

throw error when before middleware returns undefined #49

capaj opened this issue Jan 13, 2017 · 9 comments

Comments

@capaj
Copy link
Collaborator

capaj commented Jan 13, 2017

I'd think this would be safer for most programmers who are like me and forget the return

@ianaya89
Copy link
Contributor

ianaya89 commented Jan 13, 2017

Is not a bad idea, I think we can add a warning at least. We can consider same for after mid. @gillchristian @ndelvalle any thoughts?

@gillchristian
Copy link
Member

Yup, I've been thinking about warnings. I don't know if throwing is the way to go, but a warning surely is.

We could try something similar to what React does with propTypes checking only on development build.

@ndelvalle
Copy link
Member

Yeah, I don't think that throwing is the way to go. A warning sounds good, but just for a dev build like @gillchristian suggested. The thing is that if we do it, we should do it for all functionalities, cause users that use this dev build will expect warnings for all functionalities used in a wrong way.

@ianaya89
Copy link
Contributor

Makes sense, I like the idea, will improve devs experience and is a way to guide new devs how to use the lib. I would like to start working on this.

@gillchristian
Copy link
Member

gillchristian commented Jan 13, 2017

Awesome @ianaya89 !!!

Here is what I mean't about React doing stuff only on the development build. As I understand this can be done with rollup & browserify too, if people ends up using other bundler.

@ianaya89 ianaya89 self-assigned this Jan 13, 2017
@capaj
Copy link
Collaborator Author

capaj commented Jan 13, 2017

@gillchristian is there a usecase when returning undefined is a valid value? Seemed to me there was not.

@gillchristian
Copy link
Member

  • before: expected (the last one*) to return the config for the request.
  • after: is up to the user consuming the response. Maybe not even a warning necessary 🤔
  • finally: does not matter because middlewares are just run one after the other, no params are passed, nor is expected for them to return something.

(*) Let's consider the next case:

const noOp = () => {}

const overwriteConf  = () => ({
  headers: { 'Content-Type': 'application/json' }
  mode: 'no-cors'
})

trae.before(noOp)
trae.before(overwriteConf)

noOp runs first and sets the config object to undefined, overwriteConfgets that undefined "configuration object" but does nothing with it, instead it returns a proper configuration object.
I know this is a silly example, I don't imagine people doing stuff like this but it still shows that validation needs to be done to the returned value of the last middleware applied.

@gillchristian
Copy link
Member

gillchristian commented Jan 15, 2017

@ianaya89 check this out. It follows up with the wepack's DefinePlugin I sent in the previous comment.

@ndelvalle
Copy link
Member

It would be nice if we dont even have statements like the following in the prod bundle.

if (process.env.NODE_ENV !== 'production') {
  // check and do some warnings 
}

@gillchristian gillchristian added this to the 2.0 milestone Aug 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants