File: error.go

package info (click to toggle)
golang-github-viant-toolbox 0.33.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,280 kB
  • sloc: makefile: 16
file content (77 lines) | stat: -rw-r--r-- 1,562 bytes parent folder | download | duplicates (2)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package toolbox

import (
	"fmt"
	"io"
	"strings"
)

//NilPointerError represents nil pointer error
type NilPointerError struct {
	message string
}

//Error returns en error
func (e *NilPointerError) Error() string {
	if e.message == "" {
		return "NilPointerError"
	}
	return e.message
}

//NewNilPointerError creates a new nil pointer error
func NewNilPointerError(message string) error {
	return &NilPointerError{
		message: message,
	}
}

//IsNilPointerError returns true if error is nil pointer
func IsNilPointerError(err error) bool {
	if err == nil {
		return false
	}
	_, ok := err.(*NilPointerError)
	return ok
}

//IsEOFError returns true if io.EOF
func IsEOFError(err error) bool {
	if err == nil {
		return false
	}
	return err == io.EOF
}

//NotFoundError represents not found error
type NotFoundError struct {
	URL string
}

func (e *NotFoundError) Error() string {
	return fmt.Sprintf("not found: %v", e.URL)
}

//IsNotFoundError checks is supplied error is NotFoundError type
func IsNotFoundError(err error) bool {
	if err == nil {
		return false
	}
	_, ok := err.(*NotFoundError)
	return ok
}

//ReclassifyNotFoundIfMatched reclassify error if not found
func ReclassifyNotFoundIfMatched(err error, URL string) error {
	if err == nil {
		return nil
	}
	message := strings.ToLower(err.Error())
	if strings.Contains(message, "doesn't exist") ||
		strings.Contains(message, "no such file or directory") ||
		strings.Contains(err.Error(), "404") ||
		strings.Contains(err.Error(), "nosuchbucket") {
		return &NotFoundError{URL: URL}
	}
	return err
}