forked from vanadium-archive/go.v23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_error.go
32 lines (29 loc) · 842 Bytes
/
split_error.go
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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package syncql
import (
"strconv"
"strings"
)
// SplitError splits an error message into an offset and the remaining (i.e.,
// rhs of offset) message.
// The query error convention is "<module><optional-rpc>[offset]<remaining-message>".
// If err is nil, (0, "") are returned.
func SplitError(err error) (int64, string) {
if err == nil {
return 0, ""
}
errMsg := err.Error()
idx1 := strings.Index(errMsg, "[")
idx2 := strings.Index(errMsg, "]")
if idx1 == -1 || idx2 == -1 {
return 0, errMsg
}
offsetString := errMsg[idx1+1 : idx2]
offset, err := strconv.ParseInt(offsetString, 10, 64)
if err != nil {
return 0, errMsg
}
return offset, errMsg[idx2+1:]
}