-
Notifications
You must be signed in to change notification settings - Fork 405
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
Fix 404 text content-type for JSON controllers #241
Fix 404 text content-type for JSON controllers #241
Conversation
src/driver/express/ExpressDriver.ts
Outdated
} else if (action.successHttpCode) { | ||
if (result === undefined) { | ||
if (action.undefinedResultCode) { | ||
if (action.undefinedResultCode instanceof Function) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it has been before, but yet this is so bad naming. I would expect it to be number only. We should do some refactoring later.
I have read the code only (no testing) but it seems ok to me.
I really think we should add
Others? I wanted to draft a PR for this quite a while. |
I was thinking about |
I dont know how the default errors look like. The first thing was to swap them for custom errors and add an error middleware. 😄 But yes a |
hmm why |
if (action.undefinedResultCode instanceof Function) | ||
throw new (action.undefinedResultCode as any)(options); | ||
|
||
options.response.status(action.undefinedResultCode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you removed this code. Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay looks like it moved to the bottom?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this place I only left checking undefinedResultCode
. However now it's not about set http status code
but I decided to not move it to the bottom handler to keep consistency with the place of throwing undefinedResultCode
and nullResultCode
in the koa driver.
src/driver/express/ExpressDriver.ts
Outdated
} | ||
options.next(); | ||
} | ||
else if (result != null) { // send regular result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!==
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result != null
is an equivalent for result !== null || result !== undefined
I should change the order of the checks, so the last else
would be for send regular result
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah it also equivalent of result != 0 and others. Better if you do result !== null || result !== undefined
if its want you want to do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see? I thought it is! I wanted users to prevent from confusion but was already confused by myself. Please change to explicit checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've already done it (9b70134) + changed the order of if-else tree for more understandable code 😉
Im afraid of these changes TBH. You changed the most fragile part of code. Are you sure about your changes? Have you tested everything? |
@pleerock It's just a refactor of this part. The only change in behavior is throwing |
@NoNameProvided |
Do you ask about the 404 error? We should create error message to every error type. I would go with some more general one like |
Do you mean the default error message for every bulit-in http error subclass? It could be nice 😉 However I always prefer custom messages like: const playlistItem = await this.playlistItemRepository.findByIdAndOwner(itemId, user);
if (!playlistItem) {
throw new NotFoundError(`Playlist item with ID '${itemId}' doesn't exist!`);
} But the default message So, @NoNameProvided please review this PR, and we will wait for pleerock's acceptation as he is "afraid of these changes TBH" 😉 |
Yes
Yes, its a good idea. It's own my todolist to make error handling swappable / extendable in routing-controllers. I just dont have time to do it.
With just reading the code it looks good to me. |
@NoNameProvided looks like @pleerock is gone - if you are not afraid of this potentially dangerous changes, please merge this PR 😉 |
It looks like this PR broke the ability to write directly to the response: https://github.com/pleerock/routing-controllers#using-request-and-response-objects |
@fabiob There's now a race between routing-controllers action result handling and user's action code. We will fix this soon. |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Solving #231.
I decided to throw
NotFoundError
whenundefined
is returned from an action. This should let error handler decide about formatting the JSON response for JSON controllers and text/plain or html for text responses.However I think we should place the info about the route where the undefined has been returned. @pleerock @NoNameProvided do you have any idea about what info and how formated the error message should be? 😕