From 7e4494256a08f585523e01b1bbc51f41ff4e2b95 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Mon, 26 Sep 2022 14:50:22 +0200 Subject: Refactor: Errors into custom export types and expose types --- internal/config/config.go | 2 +- internal/discovery/discovery.go | 2 +- internal/http/http.go | 2 +- internal/log/log.go | 2 +- internal/oauth/oauth.go | 2 +- internal/server/api.go | 2 +- internal/server/common.go | 2 +- internal/server/instituteaccess.go | 2 +- internal/server/secureinternet.go | 2 +- internal/types/error.go | 116 ------------------------------------- internal/types/server.go | 69 ---------------------- internal/util/util.go | 2 +- internal/verify/verify.go | 2 +- internal/wireguard/wireguard.go | 2 +- 14 files changed, 12 insertions(+), 197 deletions(-) delete mode 100644 internal/types/error.go delete mode 100644 internal/types/server.go (limited to 'internal') diff --git a/internal/config/config.go b/internal/config/config.go index 18e466a..0965998 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "path" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" "github.com/eduvpn/eduvpn-common/internal/util" ) diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index a3877f5..e7270ea 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -6,7 +6,7 @@ import ( "time" "github.com/eduvpn/eduvpn-common/internal/http" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" "github.com/eduvpn/eduvpn-common/internal/util" "github.com/eduvpn/eduvpn-common/internal/verify" ) diff --git a/internal/http/http.go b/internal/http/http.go index f9dafbb..3a81eb6 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" ) type URLParameters map[string]string diff --git a/internal/log/log.go b/internal/log/log.go index d6a7373..c0e9c7d 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -6,7 +6,7 @@ import ( "os" "path" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" "github.com/eduvpn/eduvpn-common/internal/util" ) diff --git a/internal/oauth/oauth.go b/internal/oauth/oauth.go index 456c854..f4eacbc 100644 --- a/internal/oauth/oauth.go +++ b/internal/oauth/oauth.go @@ -11,7 +11,7 @@ import ( "time" httpw "github.com/eduvpn/eduvpn-common/internal/http" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" "github.com/eduvpn/eduvpn-common/internal/util" ) diff --git a/internal/server/api.go b/internal/server/api.go index d824c4a..4648a8f 100644 --- a/internal/server/api.go +++ b/internal/server/api.go @@ -10,7 +10,7 @@ import ( "time" httpw "github.com/eduvpn/eduvpn-common/internal/http" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" ) func APIGetEndpoints(baseURL string) (*ServerEndpoints, error) { diff --git a/internal/server/common.go b/internal/server/common.go index eaa8cdf..7f4a0de 100644 --- a/internal/server/common.go +++ b/internal/server/common.go @@ -5,7 +5,7 @@ import ( "time" "github.com/eduvpn/eduvpn-common/internal/oauth" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" "github.com/eduvpn/eduvpn-common/internal/util" "github.com/eduvpn/eduvpn-common/internal/wireguard" ) diff --git a/internal/server/instituteaccess.go b/internal/server/instituteaccess.go index aaabeb1..c5b58ef 100644 --- a/internal/server/instituteaccess.go +++ b/internal/server/instituteaccess.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/eduvpn/eduvpn-common/internal/oauth" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" ) // An instute access server diff --git a/internal/server/secureinternet.go b/internal/server/secureinternet.go index 2fbb143..3981022 100644 --- a/internal/server/secureinternet.go +++ b/internal/server/secureinternet.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/eduvpn/eduvpn-common/internal/oauth" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" "github.com/eduvpn/eduvpn-common/internal/util" ) diff --git a/internal/types/error.go b/internal/types/error.go deleted file mode 100644 index 0b3feae..0000000 --- a/internal/types/error.go +++ /dev/null @@ -1,116 +0,0 @@ -package types - -import ( - "encoding/json" - "errors" - "fmt" -) - -type ErrorLevel int8 - -const ( - // All other errors - ERR_OTHER ErrorLevel = iota - - // The error is just here as additional info - ERR_INFO -) - -type WrappedErrorMessage struct { - Level ErrorLevel - Message string - Err error -} - -func (e *WrappedErrorMessage) Unwrap() error { - return e.Err -} - -func (e *WrappedErrorMessage) Cause() error { - causeErr := e.Err - for errors.Unwrap(causeErr) != nil { - causeErr = errors.Unwrap(causeErr) - } - return causeErr -} - -func (e *WrappedErrorMessage) Traceback() string { - returnStr := fmt.Sprintf("%s\n%s", e.Message, "Traceback:") - causeErr := e.Err - for errors.Unwrap(causeErr) != nil { - causeErr = errors.Unwrap(causeErr) - var wrappedErr *WrappedErrorMessage - - errorStr := causeErr.Error() - - if errors.As(causeErr, &wrappedErr) { - errorStr = wrappedErr.Message - } - returnStr += fmt.Sprintf("\n - %s", errorStr) - } - return returnStr -} - -func (e *WrappedErrorMessage) Error() string { - return fmt.Sprintf("Got error: %s, with cause: %s", e.Message, e.Err) -} - -func GetErrorTraceback(err error) string { - var wrappedErr *WrappedErrorMessage - - if errors.As(err, &wrappedErr) { - return wrappedErr.Traceback() - } - return err.Error() -} - -func GetErrorCause(err error) error { - var wrappedErr *WrappedErrorMessage - - if errors.As(err, &wrappedErr) { - return wrappedErr.Cause() - } - return err -} - -func GetErrorLevel(err error) ErrorLevel { - var wrappedErr *WrappedErrorMessage - - if errors.As(err, &wrappedErr) { - return wrappedErr.Level - } - return ERR_OTHER -} - -type WrappedErrorMessageJSON struct { - Level ErrorLevel `json:"level"` - Cause string `json:"cause"` - Traceback string `json:"traceback"` -} - -func GetErrorJSONString(err error) (string, error) { - var wrappedErr *WrappedErrorMessage - - var level ErrorLevel - var cause error - var traceback string - - if errors.As(err, &wrappedErr) { - level = wrappedErr.Level - cause = wrappedErr.Cause() - traceback = wrappedErr.Traceback() - } else { - level = ERR_OTHER - cause = err - traceback = err.Error() - } - - json, jsonErr := json.Marshal( - &WrappedErrorMessageJSON{Level: level, Cause: cause.Error(), Traceback: traceback}, - ) - - if jsonErr != nil { - return "", jsonErr - } - return string(json), nil -} diff --git a/internal/types/server.go b/internal/types/server.go deleted file mode 100644 index 48f94fb..0000000 --- a/internal/types/server.go +++ /dev/null @@ -1,69 +0,0 @@ -package types - -import ( - "encoding/json" - "time" -) - -// Shared server types - -// Structs that define the json format for -// url: "https://disco.eduvpn.org/v2/organization_list.json" -type DiscoveryOrganizations struct { - Version uint64 `json:"v"` - List []DiscoveryOrganization `json:"organization_list"` - Timestamp time.Time `json:"go_timestamp"` - RawString string `json:"go_raw_string"` -} - -type DiscoveryOrganization struct { - DisplayName DiscoMapOrString `json:"display_name"` - OrgId string `json:"org_id"` - SecureInternetHome string `json:"secure_internet_home"` - KeywordList DiscoMapOrString `json:"keyword_list"` -} - -// Structs that define the json format for -// url: "https://disco.eduvpn.org/v2/server_list.json" -type DiscoveryServers struct { - Version uint64 `json:"v"` - List []DiscoveryServer `json:"server_list"` - Timestamp time.Time `json:"go_timestamp"` - RawString string `json:"go_raw_string"` -} - -type DiscoMapOrString map[string]string - -// The display name can either be a map or a string in the server list -// Unmarshal it by first trying a string and then the map -func (DN *DiscoMapOrString) UnmarshalJSON(data []byte) error { - var displayNameString string - - err := json.Unmarshal(data, &displayNameString) - - if err == nil { - *DN = map[string]string{"en": displayNameString} - return nil - } - - var resultingMap map[string]string - - err = json.Unmarshal(data, &resultingMap) - - if err == nil { - *DN = resultingMap - return nil - } - return err -} - -type DiscoveryServer struct { - AuthenticationURLTemplate string `json:"authentication_url_template"` - BaseURL string `json:"base_url"` - CountryCode string `json:"country_code"` - DisplayName DiscoMapOrString `json:"display_name,omitempty"` - KeywordList DiscoMapOrString `json:"keyword_list"` - PublicKeyList []string `json:"public_key_list"` - Type string `json:"server_type"` - SupportContact []string `json:"support_contact"` -} diff --git a/internal/util/util.go b/internal/util/util.go index 0af89ac..f9e2f7b 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" ) func EnsureValidURL(s string) (string, error) { diff --git a/internal/verify/verify.go b/internal/verify/verify.go index 47f5187..458e5e5 100644 --- a/internal/verify/verify.go +++ b/internal/verify/verify.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/jedisct1/go-minisign" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" ) // Verify verifies the signature (.minisig file format) on signedJson. diff --git a/internal/wireguard/wireguard.go b/internal/wireguard/wireguard.go index 52b9102..3d3ae8e 100644 --- a/internal/wireguard/wireguard.go +++ b/internal/wireguard/wireguard.go @@ -4,7 +4,7 @@ import ( "fmt" "regexp" - "github.com/eduvpn/eduvpn-common/internal/types" + "github.com/eduvpn/eduvpn-common/types" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) -- cgit v1.2.3