Skip to content

Commit

Permalink
finx mongodb
Browse files Browse the repository at this point in the history
Signed-off-by: Login Victor <batazor111@gmail.com>
  • Loading branch information
batazor committed Aug 7, 2020
1 parent 8360d2d commit 684393b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 12 additions & 2 deletions internal/store/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,12 @@ func (m *MongoLinkList) singleWrite(ctx context.Context, source *link.Link) (*li

_, err = collection.InsertOne(ctx, &data)
if err != nil {
return nil, &link.NotFoundError{Link: data, Err: fmt.Errorf("Failed marsharing link: %s", data.Url)}
switch err.(mongo.WriteException).WriteErrors[0].Code {
case 11000:
return nil, &link.NotUniqError{Link: data, Err: fmt.Errorf("Duplicate URL: %s", data.Url)}
default:
return nil, &link.NotFoundError{Link: data, Err: fmt.Errorf("Failed marsharing link: %s", data.Url)}
}
}

return data, nil
Expand All @@ -270,7 +275,12 @@ func (m *MongoLinkList) batchWrite(ctx context.Context, sources []*link.Link) ([

_, err := collection.InsertMany(ctx, docs)
if err != nil {
return nil, &link.NotFoundError{Link: sources[0], Err: fmt.Errorf("Failed marsharing link: %s", sources[0].Url)}
switch err.(mongo.WriteException).WriteErrors[0].Code {
case 11000:
return nil, &link.NotUniqError{Link: sources[0], Err: fmt.Errorf("Duplicate URL: %s", sources[0].Url)}
default:
return nil, &link.NotFoundError{Link: sources[0], Err: fmt.Errorf("Failed marsharing link: %s", sources[0].Url)}
}
}

return sources, nil
Expand Down
9 changes: 9 additions & 0 deletions pkg/link/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ type NotFoundError struct { // nolint unused
func (e *NotFoundError) Error() string {
return fmt.Sprintf("Not found link: %s", e.Link.Url)
}

type NotUniqError struct { // nolint unused
Link *Link
Err error
}

func (e *NotUniqError) Error() string {
return fmt.Sprintf("Not uniq link: %s", e.Link.Url)
}

0 comments on commit 684393b

Please sign in to comment.