You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently the api-server sends errors messages back to the client in at least two ways. Either as a JSON response or as a query param. The former take the following shape:
however, Fastify's default error responses are like this:
{
"statusCode": 400,
"error": "Bad Request",
"message": "body/profileUI must have required property 'isLocked'"
}
which is useful for debugging purposes.
However, we cannot send both JSON responses simultaneously. Also, we cannot merge the two, since we'd either lose the user friendly message (flash) or the detailed error message.
Issues to resolve
What do we want long term? Some combined object? For example:
It might be nice to have a createResponse(code, message) function we had to use everywhere.
functioncreateResponse(code: number,message?: string): ReplyObject{// Attach code, use message or defaultreturn{ ... };}
After thinking about it, the flash system seems like something that should be entirely handled (logicked) on the client's side. The client should deal with defaults, and what kind of flash to show based on the error code and/or response message.
Otherwise, that's pretty much what we already have.
Well, yes. For the most part. However, I do not think we should be sending flash.<KIND> in message. We should send the raw info, and the client should be responsible for the "user readable" message.
Maybe, yeah. It depends if the client always has the info it needs (when you take the error code into account). I suspect that's not always the case, though.
If it is the case, I'm all for letting the client handle that logic.
I agree that, ideally, the client should handle what is displayed based on the response. The client should know what the user was attempting to do - i.e. Click "Save Privacy Settings" -> error -> display "Something went wrong trying to save your privacy settings"
I think this would simplify the API. We should bring it up in the next meeting. I wonder if there's any messages that the server really does need to be in charge of. I can't think of any, but maybe... yea, I got nothin.
Reacted by Oliver Eyton-Williams, Shaun Hamilton and Huyen Nguyen
(I don't have enough context of the topic, but wanted to chime in anyway 😄)
I think moving the display to the client side is also easier for localization as error/success messages would need to be translated as well.
Though with statusCode and error alone, I don't think the client would be able to map to a user-friendly message, so we would still need errorMessage for additional information. Or if it's possible, maybe we could replace errorMessage with an errorReason property instead, which could be an enum and used in other operations on the server side.
Thinking about this more due to the Microsoft stuff where a single request can fail in so many ways, I think it would be nice to take a chapter out of IDLs, and have our error enums be something like:
// utils/shareable-errors.ts// This will be defined by the API, but can be consumed by any clientsenumMsError{InvalidLinkDomain="The URL should match the 'learn.microsoft.com' domain",UncompletedModule="One or more modules for this trophy have not been completed at 'learn.microsoft.com'"}
Then, during the build, the shareable-errors.ts would be transpiled into:
// Translatable error messages
{
"errors": [
{
"code": 0,
"name": "InvalidLinkDomain",
"msg": "The URL should match the..."
},
{
"code": 1,
"name": "UncompletedModule",
"msg": "One or more..."
}
]
}
Perhaps that's the way to go. In theory, I like it - but just kind of thinking out loud...
A possible response:
{
"statusCode": 400,
"error": "Bad Request",
"message": "body/profileUI must have required property 'isLocked'"
}
So then on the client we would have a lookup object or something?
{
"body/profileUI must have required property 'isLocked'": "flash.is-locked-err"
}
Seems like an extra step - but not terrible I guess. Do we know that the client will have access to all the variables? We have this endpoint for the MS trophies:
Then, we just need to publish our .json file with map to possible responses. Also, it would probably be better if we published some helper/package to use our custom format.
We shouldn't forget the human readable messages. 4xx.ms.trophy-3 doesn't mean anything to me.
We would still include whatever default comes with, but without duplicating info in two files, we either:
a) Have 4xx.ms.trophy-3 and map it to a responses.json file
b) Have fancy tooling that can automatically build all our responses { message: "Our usual format {{msUsername}}" } into a responses.json file for clients to consume.
I don't think I fully understand what some of these suggestions will look like - I still kinda like my earlier suggestion. Having the flash property in the response is more like a suggestion from the server - it's still up to the client to determine what to do with it. Also, it should be a pretty easy refactor on the client.
In most (maybe all) cases, this is accurate - but there may be an instance where we do want an 'info' style message when there's a 4xx error. The client could still override it and display the info style - but that seems like some potentially unnecessary extra logic on the client. It just seems simple to send a flash to client and then the client can just display it.
I'm not sure if this needs to come into the discussion, but we do have endpoints that redirect. Could we make those more restful, by maybe adding a redirect property, and let the client do the redirecting? Or maybe we want to try and get rid of those or something.
Whatever we decide, I would vote for whatever is the simplest, easiest to understand, and easiest to develop on.
We recently got an issue where the APIs return flash type as error but the client was expecting danger, causing an improper flash display: #51946.
I'm wondering if the scope of this issue includes standardizing the value of flash type as well.
If we go with error, we will need to update the Alert component in ui-components, which I don't mind doing. IMHO, error is more suitable than danger, even in the context of the component library alone.
After more thinking: The API should not be in charge of any i18n. It should send through a message developers (clients) can understand, then it is up to the client what end-user string should be displayed.
Otherwise, the API has to guess (think of) all the possible ways a client might use an endpoint in order to give an end-user the most useful message.
Practically, this means the API does not even need to provide more than { standardError, customMessageForDevelopers }, where standardError is probably an instance of Error (i.e. just what you get when something throws).
Also, we should not be providing any native (custom to) Fastify output in this response, because:
a) The client should not need to be aware we use Fastify
b) We should be agnostic to this level of tooling we use - easier migrations
I found this issue still exists in the current codebase.
In src/components/Flash/index.tsx line 20, the workaround
mapping 'error' to 'danger' is still in place. The TODO
comment confirms this needs standardization at the API level.
Happy to help test any fix for this.
Activity
ShaunSHamilton commentedon Apr 25, 2023
It might be nice to have a
createResponse(code, message)function we had to use everywhere.After thinking about it, the flash system seems like something that should be entirely handled (logicked) on the client's side. The client should deal with defaults, and what kind of flash to show based on the error code and/or response message.
ojeytonwilliams commentedon Apr 25, 2023
I.e. we can drop the 'type', because the client can see the status code? Sounds good.
Otherwise, that's pretty much what we already have. Though possibly not with a sensible default.
ShaunSHamilton commentedon Apr 25, 2023
Well, yes. For the most part. However, I do not think we should be sending
flash.<KIND>inmessage. We should send the raw info, and the client should be responsible for the "user readable" message.ojeytonwilliams commentedon Apr 25, 2023
Maybe, yeah. It depends if the client always has the info it needs (when you take the error code into account). I suspect that's not always the case, though.
If it is the case, I'm all for letting the client handle that logic.
moT01 commentedon Apr 28, 2023
I agree that, ideally, the client should handle what is displayed based on the response. The client should know what the user was attempting to do - i.e. Click "Save Privacy Settings" -> error -> display "Something went wrong trying to save your privacy settings"
I think this would simplify the API. We should bring it up in the next meeting. I wonder if there's any messages that the server really does need to be in charge of. I can't think of any, but maybe... yea, I got nothin.
Viksil11 commentedon Apr 30, 2023
huyenltnguyen commentedon May 1, 2023
(I don't have enough context of the topic, but wanted to chime in anyway 😄)
I think moving the display to the client side is also easier for localization as error/success messages would need to be translated as well.
Though with
statusCodeanderroralone, I don't think the client would be able to map to a user-friendly message, so we would still neederrorMessagefor additional information. Or if it's possible, maybe we could replaceerrorMessagewith anerrorReasonproperty instead, which could be an enum and used in other operations on the server side.ShaunSHamilton commentedon Aug 30, 2023
Thinking about this more due to the Microsoft stuff where a single request can fail in so many ways, I think it would be nice to take a chapter out of IDLs, and have our error enums be something like:
Then, during the build, the
shareable-errors.tswould be transpiled into:moT01 commentedon Sep 29, 2023
Stick the flash in the response as its own property?
ShaunSHamilton commentedon Sep 29, 2023
I think we should be going for as little information as required.
I would drop almost everything that is not default (Fastify/prisma), because the client should already be aware of the variables (request data).
moT01 commentedon Sep 29, 2023
Perhaps that's the way to go. In theory, I like it - but just kind of thinking out loud...
A possible response:
So then on the client we would have a lookup object or something?
Seems like an extra step - but not terrible I guess. Do we know that the client will have access to all the variables? We have this endpoint for the MS trophies:
The client should have their MsUsername - but if it's different than the one found in the database, it could help us figure out what is going wrong?
ShaunSHamilton commentedon Sep 29, 2023
Good point. We should definitely be returning the relevant information. I suppose the current
{message, variables}format is ok to keep.We should not be telling the client what to do, though (
flash...). We could just havemessage: "4xx.ms.trophy-3":{ "4xx": { "ms": { "trophy-3": "Our usual format {{msUsername}}" } } }Then, we just need to publish our
.jsonfile with map to possible responses. Also, it would probably be better if we published some helper/package to use our custom format.ojeytonwilliams commentedon Sep 29, 2023
We shouldn't forget the human readable messages.
4xx.ms.trophy-3doesn't mean anything to me.ShaunSHamilton commentedon Sep 29, 2023
We would still include whatever default comes with, but without duplicating info in two files, we either:
a) Have
4xx.ms.trophy-3and map it to aresponses.jsonfileb) Have fancy tooling that can automatically build all our responses
{ message: "Our usual format {{msUsername}}" }into aresponses.jsonfile for clients to consume.The only reason for this rigmarole is for i18n.
ojeytonwilliams commentedon Sep 29, 2023
Sorry, I wasn't talking about the i18n. That's all reasonable, but I think we'll still want a message that we, as developers, can understand.
So, in this hypothetical, we'd have
That way we can see what the problem is without having to dig into the json.
ShaunSHamilton commentedon Sep 29, 2023
Right,
Because then we would need to have:
ojeytonwilliams commentedon Sep 29, 2023
I doubt it would be duplicated, because we'd want to send different info to developers and to users.
moT01 commentedon Sep 30, 2023
I don't think I fully understand what some of these suggestions will look like - I still kinda like my earlier suggestion. Having the
flashproperty in the response is more like a suggestion from the server - it's still up to the client to determine what to do with it. Also, it should be a pretty easy refactor on the client.In most (maybe all) cases, this is accurate - but there may be an instance where we do want an 'info' style message when there's a 4xx error. The client could still override it and display the info style - but that seems like some potentially unnecessary extra logic on the client. It just seems simple to send a
flashto client and then the client can just display it.I'm not sure if this needs to come into the discussion, but we do have endpoints that redirect. Could we make those more restful, by maybe adding a
redirectproperty, and let the client do the redirecting? Or maybe we want to try and get rid of those or something.Whatever we decide, I would vote for whatever is the simplest, easiest to understand, and easiest to develop on.
huyenltnguyen commentedon Oct 24, 2023
We recently got an issue where the APIs return flash
typeaserrorbut the client was expectingdanger, causing an improper flash display: #51946.I'm wondering if the scope of this issue includes standardizing the value of flash
typeas well.If we go with
error, we will need to update theAlertcomponent inui-components, which I don't mind doing. IMHO,erroris more suitable thandanger, even in the context of the component library alone.freeCodeCamp/tools/ui-components/src/alert/alert.tsx
Line 3 in 7b8ff9e
ShaunSHamilton commentedon Oct 24, 2023
After more thinking: The API should not be in charge of any i18n. It should send through a message developers (clients) can understand, then it is up to the client what end-user string should be displayed.
Otherwise, the API has to guess (think of) all the possible ways a client might use an endpoint in order to give an end-user the most useful message.
Practically, this means the API does not even need to provide more than
{ standardError, customMessageForDevelopers }, wherestandardErroris probably an instance ofError(i.e. just what you get when something throws).Also, we should not be providing any native (custom to) Fastify output in this response, because:
a) The client should not need to be aware we use Fastify
b) We should be agnostic to this level of tooling we use - easier migrations
moT01 commentedon Apr 30, 2024
This feels post-mvp. Moved it to there, let me know if I shouldn't have.
KhiraHamdi commentedon May 24, 2026
I found this issue still exists in the current codebase.
In src/components/Flash/index.tsx line 20, the workaround
mapping 'error' to 'danger' is still in place. The TODO
comment confirms this needs standardization at the API level.
Happy to help test any fix for this.