aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@protonmail.ch>2022-03-06 00:51:36 +0530
committerVidhu Kant Sharma <vidhukant@protonmail.ch>2022-03-06 00:51:36 +0530
commitb6f42d806d0e0f5e59c7615cd1558e75c5b314f1 (patch)
treec9c014759f62c44520be305a60546d08140876f4
parent95b8ab702708538ccaf26efd141b448148ac6d6d (diff)
Exported all the errors so programs using this library can customize error messages.
-rw-r--r--anime/anime.go10
-rw-r--r--errhandlers/errhandlers.go15
-rw-r--r--errhandlers/errors.go38
-rw-r--r--manga/manga.go3
-rw-r--r--user/anime/animelist.go5
-rw-r--r--user/anime/update_animelist.go17
-rw-r--r--user/manga/mangalist.go5
-rw-r--r--user/manga/update_mangalist.go19
-rw-r--r--util/url_generator.go4
9 files changed, 76 insertions, 40 deletions
diff --git a/anime/anime.go b/anime/anime.go
index 292abe2..4247381 100644
--- a/anime/anime.go
+++ b/anime/anime.go
@@ -18,7 +18,6 @@ package anime
import (
"encoding/json"
- "errors"
"fmt"
"strconv"
e "github.com/MikunoNaka/MAL2Go/errhandlers"
@@ -88,8 +87,7 @@ func (c Client) GetAnimeById(animeId int, fields []string) (Anime, error) {
BASE_URL + "/" + strconv.Itoa(animeId),
[]string{"fields"},
/* it seems to still return all fields from the API.
- * this might be an issue with MAL itself
- * TODO: look into this */
+ * this might be an issue with MAL itself */
[][]string{fields},
true,
)
@@ -118,7 +116,7 @@ func (c Client) GetAnimeRanking(rankingType string, limit, offset int, fields []
// if ranking type is invalid
if !e.IsValidRankingType(rankingType) {
- return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Invalid ranking type specified: \"%s\"", rankingType))
+ return animeRanking, e.InvalidRankingError
}
endpoint, _ := u.UrlGenerator(
@@ -175,12 +173,12 @@ func (c Client) GetSeasonalAnime(year, season, sort string, limit, offset int, f
// checks if valid season is specified
if !e.IsValidSeason(season) {
- return seasonalAnime, errors.New(fmt.Sprintf("GetSeasonalAnime: Invalid season specified: \"%s\"", season))
+ return seasonalAnime, e.InvalidSeasonError
}
// checks if valid sort is specified
if !e.IsValidSeasonalSort(sort) {
- return seasonalAnime, errors.New(fmt.Sprintf("GetSeasonalAnime: Invalid sort specified: \"%s\"", sort))
+ return seasonalAnime, e.InvalidSortError
}
endpoint, _ := u.UrlGenerator(
diff --git a/errhandlers/errhandlers.go b/errhandlers/errhandlers.go
index 5f4db25..dd5f9f8 100644
--- a/errhandlers/errhandlers.go
+++ b/errhandlers/errhandlers.go
@@ -17,8 +17,6 @@
package errhandlers
import (
- "errors"
- "fmt"
"github.com/MikunoNaka/MAL2Go/util"
)
@@ -33,7 +31,7 @@ func FieldsErrHandler(fields []string) ([]string, error) {
// checks if each given field is valid
for _, j := range(fields) {
if !IsValidField(j) {
- return []string{}, errors.New(fmt.Sprintf("InvalidFieldError: Invalid field specified: \"%s\"", j))
+ return []string{}, InvalidFieldError
}
}
@@ -52,7 +50,7 @@ func MangaFieldsErrHandler(fields []string) ([]string, error) {
// checks if each given field is valid
for _, j := range(fields) {
if !IsValidMangaField(j) {
- return []string{}, errors.New(fmt.Sprintf("InvalidFieldError: Invalid field specified: \"%s\"", j))
+ return []string{}, InvalidFieldError
}
}
@@ -63,7 +61,14 @@ func MangaFieldsErrHandler(fields []string) ([]string, error) {
// if limit or error specified are above the limit
func LimitErrHandler(limit, maxLimit int) error {
if limit > maxLimit {
- return errors.New(fmt.Sprintf("InvalidLimitError: Limit specified too high (%d > %d).", limit, maxLimit))
+ switch maxLimit {
+ case 500:
+ return InvalidLimitError500
+ case 1000:
+ return InvalidLimitError500
+ default:
+ return InvalidLimitError
+ }
}
// return nil if no error
return nil
diff --git a/errhandlers/errors.go b/errhandlers/errors.go
new file mode 100644
index 0000000..4ea4311
--- /dev/null
+++ b/errhandlers/errors.go
@@ -0,0 +1,38 @@
+/* MAL2Go - MyAnimeList V2 API wrapper for Go
+ * Copyright (C) 2022 Vidhu Kant Sharma <vidhukant@protonmail.ch>
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+package errhandlers
+
+import (
+ "errors"
+)
+
+var InvalidFieldError error = errors.New("InvalidFieldError: Invalid field specified.")
+var InvalidLimitError error = errors.New("InvalidLimitError: Limit specified too high.")
+var InvalidLimitError500 error = errors.New("InvalidLimitError: Limit specified too high. (max is 500)")
+var InvalidLimitError1000 error = errors.New("InvalidLimitError: Limit specified too high. (max is 1000)")
+
+var InvalidRankingError error = errors.New("InvalidRankingError: Invalid ranking type specified.")
+var InvalidSeasonError error = errors.New("InvalidSeasonError: Invalid season specifield.")
+var InvalidSortError error = errors.New("InvalidSortError: Invalid sort type specifield.")
+var InvalidStatusError error = errors.New("InvalidStatusError: Invalid status specified.")
+
+var URLNameValueError error = errors.New("URLNameValueError: Number of names and values passed to URLGenerator don't match.")
+
+var InvalidScoreError error = errors.New("InvalidScoreError: Score should lie between 0-10.")
+var InvalidPriorityError error = errors.New("InvalidPriorityError: Priority should lie between 0-2.")
+var InvalidRewatchValueError error = errors.New("InvalidRewatchValueError: Rewatch value should lie between 0-5.")
+var InvalidRereadValueError error = errors.New("InvalidRereadValueError: Reread value should lie between 0-5.")
diff --git a/manga/manga.go b/manga/manga.go
index 0fd7853..f50f6e2 100644
--- a/manga/manga.go
+++ b/manga/manga.go
@@ -18,7 +18,6 @@ package manga
import (
"encoding/json"
- "errors"
"fmt"
"strconv"
e "github.com/MikunoNaka/MAL2Go/errhandlers"
@@ -115,7 +114,7 @@ func (c Client) GetMangaRanking(rankingType string, limit, offset int, fields []
// if ranking type is invalid
if !e.IsValidMangaRankingType(rankingType) {
- return mangaRanking, errors.New(fmt.Sprintf("GetMangaRanking: Invalid ranking type specified: \"%s\"", rankingType))
+ return mangaRanking, e.InvalidRankingError
}
endpoint, _ := u.UrlGenerator(
diff --git a/user/anime/animelist.go b/user/anime/animelist.go
index f6d9965..2840df3 100644
--- a/user/anime/animelist.go
+++ b/user/anime/animelist.go
@@ -20,7 +20,6 @@ import (
"encoding/json"
"strconv"
"fmt"
- "errors"
a "github.com/MikunoNaka/MAL2Go/anime"
e "github.com/MikunoNaka/MAL2Go/errhandlers"
u "github.com/MikunoNaka/MAL2Go/util"
@@ -57,12 +56,12 @@ func (c Client) GetAnimeList(user, status, sort string, limit, offset int, field
// checks if valid sort is specified
if !e.IsValidListSort(sort) {
- return userAnimeList, errors.New(fmt.Sprintf("GetAnimeList: Invalid sort specified: \"%s\"", sort))
+ return userAnimeList, e.InvalidSortError
}
// checks if valid status is specified
if status != "" && !e.IsValidListStatus(status) {
- return userAnimeList, errors.New(fmt.Sprintf("GetAnimeList: Invalid status specified: \"%s\"", status))
+ return userAnimeList, e.InvalidStatusError
}
// get own list if user not specified
diff --git a/user/anime/update_animelist.go b/user/anime/update_animelist.go
index 8371691..780f079 100644
--- a/user/anime/update_animelist.go
+++ b/user/anime/update_animelist.go
@@ -17,7 +17,6 @@ package anime
import (
e "github.com/MikunoNaka/MAL2Go/errhandlers"
- "errors"
"fmt"
"net/url"
"strconv"
@@ -34,7 +33,7 @@ func (c Client)SetStatus(id int, status string) (serverResponse, error) {
// checks if specified list status is valid
if !e.IsValidListStatus(status) {
- return serverResponse{}, errors.New(fmt.Sprintf("SetStatus: Invalid list status: \"%s\"", status))
+ return serverResponse{}, e.InvalidStatusError
}
// data to be sent to the server
@@ -75,7 +74,7 @@ func (c Client)SetScore(id int, score int) (serverResponse, error) {
// checks if specified score is valid
if !e.IsValidScore(score) {
- return serverResponse{}, errors.New(fmt.Sprintf("SetScore: Invalid score: %d doesn't lie within 0-10", score))
+ return serverResponse{}, e.InvalidScoreError
}
// data to be sent to the server
@@ -92,7 +91,7 @@ func (c Client)SetPriority(id int, priority int) (serverResponse, error) {
// checks if specified priority is valid
if !e.IsValidPriority(priority) {
- return serverResponse{}, errors.New(fmt.Sprintf("SetPriority: Invalid priority: %d doesn't lie within 0-2", priority))
+ return serverResponse{}, e.InvalidPriorityError
}
// data to be sent to the server
@@ -109,7 +108,7 @@ func (c Client)SetRewatchValue(id int, rewatchValue int) (serverResponse, error)
// checks if specified rewatch value is valid
if !e.IsValidRewatchValue(rewatchValue) {
- return serverResponse{}, errors.New(fmt.Sprintf("SetRewatchValue: Invalid rewatch value: %d doesn't lie within 0-5", rewatchValue))
+ return serverResponse{}, e.InvalidRewatchValueError
}
// data to be sent to the server
@@ -165,22 +164,22 @@ func (c Client)UpdateAnime(id int, data UpdateAnimeData) (serverResponse, error)
// checks if specified list status is valid
if !e.IsValidListStatus(data.Status) {
- return serverResponse{}, errors.New(fmt.Sprintf("UpdateAnime: Invalid list status: \"%s\"", data.Status))
+ return serverResponse{}, e.InvalidStatusError
}
// checks if specified score is valid
if !e.IsValidScore(data.Score) {
- return serverResponse{}, errors.New(fmt.Sprintf("UpdateAnime: Invalid score: %d doesn't lie within 0-10", data.Score))
+ return serverResponse{}, e.InvalidScoreError
}
// checks if specified priority is valid
if !e.IsValidPriority(data.Priority) {
- return serverResponse{}, errors.New(fmt.Sprintf("UpdateAnime: Invalid priority: %d doesn't lie within 0-2", data.Priority))
+ return serverResponse{}, e.InvalidPriorityError
}
// checks if specified rewatch value is valid
if !e.IsValidRewatchValue(data.RewatchValue) {
- return serverResponse{}, errors.New(fmt.Sprintf("UpdateAnime: Invalid rewatch value: %d doesn't lie within 0-5", data.RewatchValue))
+ return serverResponse{}, e.InvalidRereadValueError
}
params := url.Values{}
diff --git a/user/manga/mangalist.go b/user/manga/mangalist.go
index 009a65c..af75e9f 100644
--- a/user/manga/mangalist.go
+++ b/user/manga/mangalist.go
@@ -20,7 +20,6 @@ import (
"encoding/json"
"strconv"
"fmt"
- "errors"
e "github.com/MikunoNaka/MAL2Go/errhandlers"
u "github.com/MikunoNaka/MAL2Go/util"
)
@@ -56,12 +55,12 @@ func (c Client) GetMangaList(user, status, sort string, limit, offset int, field
// checks if valid sort is specified
if !e.IsValidMangaListSort(sort) {
- return userMangaList, errors.New(fmt.Sprintf("GetMangaList: Invalid sort specified: \"%s\"", sort))
+ return userMangaList, e.InvalidSortError
}
// checks if valid status is specified
if status != "" && !e.IsValidMangaListStatus(status) {
- return userMangaList, errors.New(fmt.Sprintf("GetMangaList: Invalid status specified: \"%s\"", status))
+ return userMangaList, e.InvalidStatusError
}
// get own list if user not specified
diff --git a/user/manga/update_mangalist.go b/user/manga/update_mangalist.go
index 423a919..2fbbe48 100644
--- a/user/manga/update_mangalist.go
+++ b/user/manga/update_mangalist.go
@@ -17,7 +17,6 @@ package manga
import (
e "github.com/MikunoNaka/MAL2Go/errhandlers"
- "errors"
"fmt"
"net/url"
"strconv"
@@ -34,7 +33,7 @@ func (c Client)SetStatus(id int, status string) (serverResponse, error) {
// checks if specified list status is valid
if !e.IsValidMangaListStatus(status) {
- return serverResponse{}, errors.New(fmt.Sprintf("SetStatus: Invalid list status: \"%s\"", status))
+ return serverResponse{}, e.InvalidStatusError
}
// data to be sent to the server
@@ -87,7 +86,7 @@ func (c Client)SetScore(id int, score int) (serverResponse, error) {
// checks if specified score is valid
if !e.IsValidScore(score) {
- return serverResponse{}, errors.New(fmt.Sprintf("SetScore: Invalid score: %d doesn't lie within 0-10", score))
+ return serverResponse{}, e.InvalidScoreError
}
// data to be sent to the server
@@ -104,7 +103,7 @@ func (c Client)SetPriority(id int, priority int) (serverResponse, error) {
// checks if specified priority is valid
if !e.IsValidPriority(priority) {
- return serverResponse{}, errors.New(fmt.Sprintf("SetPriority: Invalid priority: %d doesn't lie within 0-2", priority))
+ return serverResponse{}, e.InvalidPriorityError
}
// data to be sent to the server
@@ -121,7 +120,7 @@ func (c Client)SetRereadValue(id int, rereadValue int) (serverResponse, error) {
// checks if specified reread value is valid
if !e.IsValidRewatchValue(rereadValue) {
- return serverResponse{}, errors.New(fmt.Sprintf("SetRereadValue: Invalid rewatch value: %d doesn't lie within 0-5", rereadValue))
+ return serverResponse{}, e.InvalidRereadValueError
}
// data to be sent to the server
@@ -177,22 +176,22 @@ func (c Client)UpdateManga(id int, data UpdateMangaData) (serverResponse, error)
// checks if specified list status is valid
if !e.IsValidMangaListStatus(data.Status) {
- return serverResponse{}, errors.New(fmt.Sprintf("UpdateManga: Invalid list status: \"%s\"", data.Status))
+ return serverResponse{}, e.InvalidStatusError
}
// checks if specified score is valid
if !e.IsValidScore(data.Score) {
- return serverResponse{}, errors.New(fmt.Sprintf("UpdateManga: Invalid score: %d doesn't lie within 0-10", data.Score))
+ return serverResponse{}, e.InvalidScoreError
}
// checks if specified priority is valid
if !e.IsValidPriority(data.Priority) {
- return serverResponse{}, errors.New(fmt.Sprintf("UpdateManga: Invalid priority: %d doesn't lie within 0-2", data.Priority))
+ return serverResponse{}, e.InvalidPriorityError
}
- // checks if specified rewatch value is valid
+ // checks if specified reread value is valid
if !e.IsValidRewatchValue(data.RereadValue) {
- return serverResponse{}, errors.New(fmt.Sprintf("UpdateManga: Invalid reread value: %d doesn't lie within 0-5", data.RereadValue))
+ return serverResponse{}, e.InvalidRereadValueError
}
params := url.Values{}
diff --git a/util/url_generator.go b/util/url_generator.go
index e6799b5..d0f5eea 100644
--- a/util/url_generator.go
+++ b/util/url_generator.go
@@ -17,13 +17,13 @@
package util
import (
- "errors"
+ e "github.com/MikunoNaka/MAL2Go/errhandlers"
)
func UrlGenerator(baseUrl string, names []string, values [][]string, isPrimary bool) (string, error) {
// length of names and values should be same
if cap(names) != cap(values) {
- return "", errors.New("util.UrlGenerator: Error: Length of names and values don't match.")
+ return "", e.URLNameValueError
}
var fields string