forked from hypermodeinc/modusGraph
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
54 lines (45 loc) · 1.41 KB
/
errors.go
File metadata and controls
54 lines (45 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph
import (
"regexp"
"strings"
dg "github.com/dolan-in/dgman/v2"
)
// UniqueError represents an error that occurs when attempting to insert or update
// a node that would violate a unique constraint.
type UniqueError = dg.UniqueError
// parseUniqueError attempts to parse a Dgraph unique constraint violation error
// and convert it to a UniqueError. Returns nil if the error is not a unique constraint violation.
func parseUniqueError(err error) *UniqueError {
if err == nil {
return nil
}
errStr := err.Error()
// Match Dgraph unique constraint error format:
// "could not insert duplicate value [Value] for predicate [predicate]"
re := regexp.MustCompile(`could not insert duplicate value \[([^\]]+)\] for predicate \[([^\]]+)\]`)
matches := re.FindStringSubmatch(errStr)
if len(matches) == 3 {
return &UniqueError{
Field: matches[2],
Value: matches[1],
}
}
// Also check for dgman's error format:
// " with field=value already exists at uid=0x..."
if strings.Contains(errStr, "already exists at uid=") {
re2 := regexp.MustCompile(`with ([^=]+)=([^ ]+) already exists at uid=([^ ]+)`)
matches2 := re2.FindStringSubmatch(errStr)
if len(matches2) == 4 {
return &UniqueError{
Field: matches2[1],
Value: matches2[2],
UID: matches2[3],
}
}
}
return nil
}