Skip to content

Fix error presentation issues - #1309

Closed
jvherweg wants to merge 2 commits into
versions/5.0.0from
fix/error-preferences
Closed

jvherweg wants to merge 2 commits into
versions/5.0.0from
fix/error-preferences

Conversation

@jvherweg

@jvherweg jvherweg commented May 11, 2022 •

Copy link
Copy Markdown
Member

📁 Related issues

#1148 (comment)

✍️ Description

Fixes several issues I still had with how we showed errors to the users. There were still several cases where the user would get a text representation of the error, even if they requested HTML or something else. This PR fixes all of those cases (I think).

The PreferenceParser has been extracted from the BasicRequestParser and moved to the ParsingHttpHandler. This way we can parse the preferences before any error gets thrown by, for example, an invalid N3 Patch body that gets parsed.

The IdentityProviderFactory now also takes a PreferenceParser as input so it can correctly create a representation for OIDC errors. The extra error metadata that gets added now only gets added if the showStackTrace parameter has been set to true to make sure we don't leak too much information. Although that is not (yet) super relevant due to #1182 .

This does overload the IdentityProviderFactory class a bit, we could always split it up in the future but I think it's fine since it hides all the configuration for the oidc-provider library.

@jvherweg
jvherweg requested a review from RubenVerborgh May 11, 2022 14:17
@RubenVerborgh RubenVerborgh added the semver.major Requires a major version bump label May 12, 2022

@RubenVerborgh RubenVerborgh left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not sit well with me. It feels like something is off.

What feels uneasy is this:

export interface RequestParserInput {
  request: HttpRequest;
  preferences: RepresentationPreferences;
}

So we have a parser that accepts a partially parsed request.
Meaning there is another parser somewhere.

I can't think of a proper name for this new RequestParser object, and I take that as a sign that there is no clear boundary of what it does and doesn't.


I think the actual problem is that some output producers need these preferences.
So in that sense, how about we instead pass request to errorHandler? After all, an error is handled in response to a request, so it's not weird for an error handler to know about the request that caused the error. Then we preference parse that request and we're done? (The only downside seems to be possible double preference parsing; caching can fix that.)


// Also change the error message in the stack trace
if (error.stack) {
error.stack = [ `${error.name}: ${error.message}`, ...error.stack.split('\n').slice(1) ].join('\n');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just error.stack.replace(/.*/, `${error.name}: ${error.message}`)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because my regex-fu is simply not strong enough.

@jvherweg

Copy link
Copy Markdown
Member Author

It does not sit well with me. It feels like something is off.

I get the issue. Passing the request object to the error handler could work, the issue there is that I don't like that the request object is still being used after we made the Operation object. Yes the error is handled in response to a request, but literally everything the server does is in response to a request. The ErrorHandler's core goal is converting an Error to a ResponseDescription.

I'll explain the solution I had first, might be relevant, but I reverted it because it felt more invasive than it needed to be and perhaps made the ParsingHttpHandler too big.

Originally I completely removed the BasicRequestParser and moved all its functionality to the ParsingHttpHandler. Which sort of makes sense since that handler parses the input. This just causes it to do the parsing itself instead of calling a RequestParser. The issue with that is that this doubles the constructor parameters for the handler from 5 to 10. To keep the amount the same, since otherwise we would have a lot of duplication in all our configs using the ParsingHttpHandler, I changed RequestParser to be a simple record-like object containing all those new parameters. This way the configs hardly change since all constructor parameters stay the same everywhere. But this does give a lot of different classes in the handler.

@Falx

Falx commented May 17, 2022

Copy link
Copy Markdown
Contributor

Just my two cents:

I've looked at all options (out of curiosity) and my preference would be alternative 2, where you are moving all the parsing to the ParsingHttpHandler. It feels to me like that has the best separation of concerns. You are also not passing around request any more than you have to. The fact that the ParsingHttpHandler now takes 10 constructor arguments is effectively mitigated by the use of RequestParser as a utility class, which is again a good way to keep concerns that belong together, close to eachother. (in my opinion).

On the original suggestion: I can follow the original concern mentioned by @RubenVerborgh here about the request object and no clear boundary between the RequestParsers that receive it.

On the first alternative: personally not a fan of passing the request object to your error handling code, just to get the representation preferences out of it (again).

@RubenVerborgh

Copy link
Copy Markdown
Member

It feels to me like that has the best separation of concerns.

Not entirely sure about that, though. I dislike that callers have to prepare the preferences; they should not know about these things.

You are also not passing around request any more than you have to.

Agreed.
However, it does make sense that an error handlers gets the full request. That could be very useful for debugging purposes if we want to add more info.

The fact that the ParsingHttpHandler now takes 10 constructor arguments is effectively mitigated by the use of RequestParser as a utility class, which is again a good way to keep concerns that belong together, close to eachother.

Does it make sense to replicate the same in alternative 1?

In any case, I feel that 1 is the way to go. My 2 cents.

@jvherweg

Copy link
Copy Markdown
Member Author

However, it does make sense that an error handlers gets the full request. That could be very useful for debugging purposes if we want to add more info.

I sort of get that, although I think it should be more the responsibility of the class throwing the error in the first place to provide relevant information about which part of the request was causing the issue. The same argument could also be used to say that we should pass the entire internal state that we have to the error handler so it can provide even more debugging information in case of error.

Does it make sense to replicate the same in alternative 1?

It's not really relevant in alternative 1 since there the RequestParser is still a separate class that handles everything related to parsing the request. We could also still do the move but there is no real need for it. And personally I'm not a big fan of this solution where the ParsingHttpHandler gets so many inputs.

I wonder if we want a full BasicRequestParser for forward compatibility (and we cache).

This is a comment from the review on alternative 1 but wanted to place it here to have the discussion in 1 place and I think it's relevant to the total picture.

It's important to remember that the main goal of this PR is to make sure we also have content negotiation on errors that get thrown before the request could be (fully) parsed. So if we use the BasicRequestParser in the ErrorHandler, we end up in the exact same situation as before that we don't know the preferences because parsing the body failed somewhere. My idea for the future was actually the opposite, if we ever have a PreferenceParser that does more than parse the accept headers, to give a simpler one to the ErrorHandler to make sure it always succeeds and we can do content negotiation.

The above makes me wonder if we should perhaps have a Partial<Operation> object that we can receive is parsing the request partially succeeded and pass that along so we at least have our preferences 😅.

In any case, I feel that 1 is the way to go. My 2 cents.

I'm OK with going for that one since it does fix the issue with not that drastic of a requirement change tbh. But I do find the discussion interesting 😄

@jvherweg

jvherweg commented Jun 1, 2022

Copy link
Copy Markdown
Member Author

Done in #1314

@jvherweg jvherweg closed this Jun 1, 2022
@jvherweg
jvherweg deleted the fix/error-preferences branch June 1, 2022 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver.major Requires a major version bump

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants