From 393336f240e29f34e9cd7183c461f38c4da8d36b Mon Sep 17 00:00:00 2001 From: Ludovico Russo Date: Thu, 28 Apr 2022 15:13:51 +0200 Subject: [PATCH] deprecate panicking functions --- object.go | 6 ++++++ vt.go | 22 +++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/object.go b/object.go index cbb55a7..6677599 100644 --- a/object.go +++ b/object.go @@ -232,6 +232,7 @@ func (obj *Object) GetInt64(attr string) (int64, error) { } // MustGetInt64 is like GetInt64, but it panic in case of error. +// Deprecated: use GetInt64 instead func (obj *Object) MustGetInt64(attr string) int64 { result, err := obj.GetInt64(attr) if err != nil { @@ -255,6 +256,7 @@ func (obj *Object) GetFloat64(attr string) (float64, error) { } // MustGetFloat64 is like GetFloat64, but it panic in case of error. +// Deprecated: use GetFloat64 instead func (obj *Object) MustGetFloat64(attr string) float64 { result, err := obj.GetFloat64(attr) if err != nil { @@ -278,6 +280,7 @@ func (obj *Object) GetString(attr string) (s string, err error) { } // MustGetString is like GetString, but it panic in case of error. +// Deprecated: use GetString instead func (obj *Object) MustGetString(attr string) string { result, err := obj.GetString(attr) if err != nil { @@ -302,6 +305,7 @@ func (obj *Object) GetTime(attr string) (t time.Time, err error) { } // MustGetTime is like GetTime, but it panic in case of error. +// Deprecated: use GetTime instead func (obj *Object) MustGetTime(attr string) time.Time { result, err := obj.GetTime(attr) if err != nil { @@ -325,6 +329,7 @@ func (obj *Object) GetBool(attr string) (b bool, err error) { } // MustGetBool is like GetTime, but it panic in case of error. +// Deprecated: use GetBool instead func (obj *Object) MustGetBool(attr string) bool { result, err := obj.GetBool(attr) if err != nil { @@ -358,6 +363,7 @@ func (obj *Object) GetStringSlice(attr string) (s []string, err error) { } // MustGetStringSlice is like GetStringSlice, but it panic in case of error. +// Deprecated: use GetStringSlice instead func (obj *Object) MustGetStringSlice(attr string) []string { result, err := obj.GetStringSlice(attr) if err != nil { diff --git a/vt.go b/vt.go index 5ec69a7..04316ec 100644 --- a/vt.go +++ b/vt.go @@ -64,20 +64,32 @@ func (e Error) Error() string { return e.Message } -// URL returns a full VirusTotal API URL from a relative path (i.e: a path +// NewURL returns a full VirusTotal API URL from a relative path (i.e: a path // without the domain name and the "/api/v3/" prefix). The path can contain // format 'verbs' as defined in the "fmt". This function is useful for creating // URLs to be passed to any function expecting a *url.URL in this library. -func URL(pathFmt string, a ...interface{}) *url.URL { +func NewURL(pathFmt string, a ...interface{}) (*url.URL, error) { path := fmt.Sprintf(pathFmt, a...) url, err := url.Parse(path) if err != nil { - msg := fmt.Sprintf( + return nil, fmt.Errorf( "error formatting URL \"%s\": %s", pathFmt, err) - panic(msg) } - return baseURL.ResolveReference(url) + return baseURL.ResolveReference(url), nil +} + +// URL returns a full VirusTotal API URL from a relative path (i.e: a path +// without the domain name and the "/api/v3/" prefix). The path can contain +// format 'verbs' as defined in the "fmt". This function is useful for creating +// URLs to be passed to any function expecting a *url.URL in this library. +// Deprecated: use NewURL instead +func URL(pathFmt string, a ...interface{}) *url.URL { + url, err := NewURL(pathFmt, a...) + if err != nil { + panic(err.Error()) + } + return url } // SetHost allows to change the host used while sending requests to the