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

Antti | Error structure changes #17

Merged
merged 20 commits into from
Mar 14, 2024
Merged

Conversation

anttiasmala
Copy link
Collaborator

Tein muutaman pienen muutoksen koskien virhekoodeja / virheiden käsittelyä. En ollut tyytyväinen tuohon vanhaan tapaan, joten ajattelin hiukan päivittää sitä. Ei mitään isoja muutoksia, mutta jotain pientä

Copy link

vercel bot commented Mar 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lahjalista ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 13, 2024 5:27pm

throw new Error('Invalid ID', { cause: 'idError' });
return res.status(400).send('Invalid ID');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tässä kohtaa mielestäni on turha heittää virhekoodia, koska emme kerää mitään dataa esimerkiksi virheiden määrästä tmv

Copy link
Owner

@samuliasmala samuliasmala Mar 7, 2024

Choose a reason for hiding this comment

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

Se etu olisi heittää virhe, että silloin virheenkäsittely tapahtuisi kokonaisuudessaan samassa paikassa. Sitten jos esim. haluaa tulostaa virheistä tiedon serverin konsoliin, niin olisi helppoa kun tulostus riittäisi lisätä yhteen paikkaan ja saisi kerralla kaikki virheet hoidettua. Itse siis heittäisin tässä HttpError virheen.

@anttiasmala
Copy link
Collaborator Author

anttiasmala commented Mar 5, 2024

Olisiko järkevämpää käydä muita Prisman virhekoodeja läpi jo tässä vaiheessa vaiko vasta myöhemmin tarvittaessa? Nyt muut Prisman virhekoodit vain palauttavat Server error!

Mulla on myös versio, jossa on oma CustomError, mutta en tiedä onko tarpeellinen vielä. Teen vaikka hiukan myöhemässä vaiheessa PR:n siitä

Edit: Lisäsin oman CustomErrorin HttpError-nimellä

@samuliasmala
Copy link
Owner

samuliasmala commented Mar 7, 2024

Olisiko järkevämpää käydä muita Prisman virhekoodeja läpi jo tässä vaiheessa vaiko vasta myöhemmin tarvittaessa? Nyt muut Prisman virhekoodit vain palauttavat Server error!

Ei kannata käydä muita Prisman virhekoodeja läpi, mutta kannattaa lisätä console.error(e); juuri ennen "Server error" -viestin palauttamista, niin saat serverin konsoliin tiedon virheestä.

Copy link
Owner

@samuliasmala samuliasmala left a comment

Choose a reason for hiding this comment

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

Hyvältä näyttää, pari kommenttia.


if (e instanceof PrismaClientKnownRequestError) {
if (e.code === 'P2025') {
return res.status(404).send('Gift was not found on the server!');
Copy link
Owner

Choose a reason for hiding this comment

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

Eikös tässä voi olla kyse myös Userista nyt kun se PR mergetty? Eli saako error-objektista jotenkin tietää kummasta on kyse? Jos ei, niin pitäisikö geneerisesti ilmoittaa, että "Object was not..." tms.?

throw new Error('Invalid ID', { cause: 'idError' });
return res.status(400).send('Invalid ID');
Copy link
Owner

@samuliasmala samuliasmala Mar 7, 2024

Choose a reason for hiding this comment

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

Se etu olisi heittää virhe, että silloin virheenkäsittely tapahtuisi kokonaisuudessaan samassa paikassa. Sitten jos esim. haluaa tulostaa virheistä tiedon serverin konsoliin, niin olisi helppoa kun tulostus riittäisi lisätä yhteen paikkaan ja saisi kerralla kaikki virheet hoidettua. Itse siis heittäisin tässä HttpError virheen.

@anttiasmala anttiasmala mentioned this pull request Mar 11, 2024
Comment on lines 15 to 18
if (e.meta?.modelName === 'Gift') {
return res.status(404).send('Gift was not found on the server!');
}
return res.status(404).send('Record was not found on the server!');
Copy link
Owner

Choose a reason for hiding this comment

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

Tekisin tästä geneerisen, joka toimii kaikilla modeleilla:

Suggested change
if (e.meta?.modelName === 'Gift') {
return res.status(404).send('Gift was not found on the server!');
}
return res.status(404).send('Record was not found on the server!');
const modelName =
typeof e.meta?.modelName === 'string' ? e.meta?.modelName : 'Record';
return res.status(404).send(modelName + ' was not found on the server!');

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Vau! Tämä oli hyvä, kiitos!! Ei käynyt edes mielessä, vaikka hyvin simppeli tapa onkin 😄

Copy link
Owner

@samuliasmala samuliasmala left a comment

Choose a reason for hiding this comment

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

Valmis mergettäväksi! 💪

@anttiasmala anttiasmala merged commit 0e1579b into main Mar 14, 2024
4 of 5 checks passed
@anttiasmala anttiasmala deleted the antti/error-structure-change branch March 14, 2024 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants