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

treat issue problem index out of bounds! #13

Merged
merged 5 commits into from
Feb 28, 2020
Merged

treat issue problem index out of bounds! #13

merged 5 commits into from
Feb 28, 2020

Conversation

joeberth
Copy link
Contributor

@joeberth joeberth commented Feb 27, 2020

#9

Copy link
Contributor

@danielfireman danielfireman left a comment

Choose a reason for hiding this comment

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

Existem duas formas de lidar com esse caso:

  • retornar um erro que de oportunidade do usuário diferenciar erro de lista vazia (geralmente o erro é uma constante exportada). Para um exemplo disso, olha a api de consulta do driver mongo e como eles tratam o caso de lista vazia.
  • retornar a resposta em forma de lista e retornar a lista vazia

Independente, também precisa estar claro na documentação

mgo.go Outdated
return nil, fmt.Errorf("there is no match with these arguments")
// ErrNoDocuments means that the filter did not match any documents in the collection
if err == mongo.ErrNoDocuments {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

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

Por favor não retorne um elemento de outro pacote que você está abstraindo do usuário. Fazendo isso você está expondo um detalhe de implementação: a forma como seu dado é armazenado.

É super simples criar uma constante dentro do seu próprio pacote e retornar ela

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Entendi, vou ter mais cuidado a partir de agora.

func (c *DBClient) GetDataForSecondScreen(month int, year int, agency string) (*AgencyMonthlyInfo, error) {
c.Collection(c.monthlyInfoCol)
resultMonthly, err := c.col.Find(context.TODO(), bson.D{{Key: "aid", Value: agency}, {Key: "year", Value: year}, {Key: "month", Value: month}})
var resultMonthly AgencyMonthlyInfo
err := c.col.FindOne(context.TODO(), bson.D{{Key: "aid", Value: agency}, {Key: "year", Value: year}, {Key: "month", Value: month}}).Decode(&resultMonthly)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
err := c.col.FindOne(context.TODO(), bson.D{{Key: "aid", Value: agency}, {Key: "year", Value: year}, {Key: "month", Value: month}}).Decode(&resultMonthly)
err := c.col.FindOne(context.TODO(), bson.D{
{Key: "aid", Value: agency},
{Key: "year", Value: year},
{Key: "month", Value: month}
}).Decode(&resultMonthly)

@joeberth joeberth merged commit 8f7ec8c into master Feb 28, 2020
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