-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
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.
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 |
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.
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
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.
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) |
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.
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) |
#9