summaryrefslogtreecommitdiff
path: root/types
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-09-26 14:50:22 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-09-26 15:33:04 +0200
commit7e4494256a08f585523e01b1bbc51f41ff4e2b95 (patch)
treeccbf873b2bfb11aa22f185e78ce1e2e5eebd094c /types
parent448c51d2142c186f0490b9d51c0d73beb3c76863 (diff)
Refactor: Errors into custom export types and expose types
Diffstat (limited to 'types')
-rw-r--r--types/error.go82
-rw-r--r--types/server.go69
2 files changed, 151 insertions, 0 deletions
diff --git a/types/error.go b/types/error.go
new file mode 100644
index 0000000..607e6c6
--- /dev/null
+++ b/types/error.go
@@ -0,0 +1,82 @@
+package types
+
+import (
+ "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
+}
diff --git a/types/server.go b/types/server.go
new file mode 100644
index 0000000..48f94fb
--- /dev/null
+++ b/types/server.go
@@ -0,0 +1,69 @@
+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"`
+}