Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions v2/arangodb/collection_documents_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,22 @@ type CollectionDocumentCreate interface {

// CreateDocuments creates multiple documents in the collection.
// The document data is loaded from the given documents slice, the documents metadata is returned.
// If a documents element already contains a `_key` field, this will be used as key of the new document,
// If a document element already contains a `_key` field, this will be used as key of the new document,
// otherwise a unique key is created.
// If a documents element contains a `_key` field with a duplicate key, other any other field violates an index constraint,
// a ConflictError is returned in its indeed in the errors slice.
// If a document element contains a `_key` field with a duplicate key, or any other field that violates an index constraint,
// then the ConflictError for a specific document will be returned only while reading from CollectionDocumentCreateResponseReader
// and not as the error output of this function.
// If the create request itself fails or one of the arguments is invalid, an error is returned.
// SmartGraphs and EnterpriseGraphs cannot use existing collections and cannot use the document interface
CreateDocuments(ctx context.Context, documents interface{}) (CollectionDocumentCreateResponseReader, error)

// CreateDocumentsWithOptions creates multiple documents in the collection.
// The document data is loaded from the given documents slice, the documents metadata is returned.
// If a documents element already contains a `_key` field, this will be used as key of the new document,
// If a document element already contains a `_key` field, this will be used as key of the new document,
// otherwise a unique key is created.
// If a documents element contains a `_key` field with a duplicate key, other any other field violates an index constraint,
// a ConflictError is returned in its indeed in the errors slice.
// If a document element contains a `_key` field with a duplicate key, or any other field that violates an index constraint,
// then the ConflictError for a specific document will be returned only while reading from CollectionDocumentCreateResponseReader
// and not as the error output of this function.
// If the create request itself fails or one of the arguments is invalid, an error is returned.
// SmartGraphs and EnterpriseGraphs cannot use existing collections and cannot use the document interface
CreateDocumentsWithOptions(ctx context.Context, documents interface{}, opts *CollectionDocumentCreateOptions) (CollectionDocumentCreateResponseReader, error)
Expand Down
20 changes: 19 additions & 1 deletion v2/arangodb/shared/error.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2017-2024 ArangoDB GmbH, Cologne, Germany
// Copyright 2017-2025 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,7 @@ import (
"fmt"
"io"
"net/http"
"regexp"
)

const (
Expand Down Expand Up @@ -423,6 +424,23 @@ func (ae ArangoError) Temporary() bool {
return ae.HasError && ae.Code == http.StatusServiceUnavailable
}

// GetConflictKey if error was caused by Conflict it returns the key that caused it or "" otherwise
func (ae ArangoError) GetConflictKey() string {
if IsConflict(error(ae)) {
// Whitespace symbols are not allowed as part of a key so trimming them will not result in a mistake.
re := regexp.MustCompile(`conflicting key:\s*(.+)$`)
match := re.FindStringSubmatch(ae.ErrorMessage)
if len(match) == 1 {
return match[1]
} else {
return ""
}

} else {
return ""
}
}

// newArangoError creates a new ArangoError with given values.
func newArangoError(code, errorNum int, errorMessage string) error {
return ArangoError{
Expand Down
4 changes: 4 additions & 0 deletions v2/arangodb/shared/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func (r ResponseStruct) AsArangoError() ArangoError {
a.ErrorMessage = *r.ErrorMessage
}

if r.ErrorMessage != nil {
a.ErrorMessage = *r.ErrorMessage
}

return a
}

Expand Down
5 changes: 4 additions & 1 deletion v2/tests/database_collection_doc_create_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2025 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@ import (
"context"
"testing"

"github.com/arangodb/go-driver/v2/arangodb/shared"
"github.com/arangodb/go-driver/v2/utils"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -77,6 +78,8 @@ func Test_DatabaseCollectionDocCreateOverwrite(t *testing.T) {
OverwriteMode: overwriteMode.New(),
})
require.Error(t, err)

require.Equal(t, meta.Key, err.(shared.ArangoError).GetConflictKey())
require.Empty(t, metaConflict.Rev)
require.Empty(t, metaConflict.Old)
require.Empty(t, metaConflict.New)
Expand Down