From 1d3f72c1b48998b86fd1740e893559b6dcaf7663 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 13:42:06 +0530 Subject: modularised the code for easy access of various functions in packages --- errhandlers/errhandlers.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 errhandlers/errhandlers.go (limited to 'errhandlers/errhandlers.go') diff --git a/errhandlers/errhandlers.go b/errhandlers/errhandlers.go new file mode 100644 index 0000000..50f4aac --- /dev/null +++ b/errhandlers/errhandlers.go @@ -0,0 +1,53 @@ +/* mal2go - MyAnimeList V2 API wrapper for Go + * Copyright (C) 2022 Vidhu Kant Sharma + + * 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 . */ + +package errhandlers + +import ( + "errors" + "fmt" + "github.com/MikunoNaka/mal2go/util" +) + +// if fields aren't specified +func FieldsErrHandler(fields []string) ([]string, error) { + if cap(fields) == 0 { + // uses all the default fields if none specified + return util.DefaultFields, nil + } + + // 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)) + } + } + + // everything's fine! + return fields, nil +} + +// if limit or error specified are above the limit +func LimitsErrHandler(limit, offset int) error { + maxOffset := 500 - limit + if limit > 500 { + return errors.New(fmt.Sprintf("InvalidLimitError: Limit specified too high (%d > 500).", limit)) + } else if offset > maxOffset { + return errors.New(fmt.Sprintf("InvalidOffsetError: Offset specified too high (%d > %d).", offset, maxOffset)) + } + // return nil if no error + return nil +} -- cgit v1.2.3 From 937f3b8ada85274dfe3842f3dde8aef45c4f3ae7 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 14:12:45 +0530 Subject: completed (?) the GetAnimeList function --- errhandlers/errhandlers.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'errhandlers/errhandlers.go') diff --git a/errhandlers/errhandlers.go b/errhandlers/errhandlers.go index 50f4aac..a9f8054 100644 --- a/errhandlers/errhandlers.go +++ b/errhandlers/errhandlers.go @@ -41,10 +41,10 @@ func FieldsErrHandler(fields []string) ([]string, error) { } // if limit or error specified are above the limit -func LimitsErrHandler(limit, offset int) error { - maxOffset := 500 - limit - if limit > 500 { - return errors.New(fmt.Sprintf("InvalidLimitError: Limit specified too high (%d > 500).", limit)) +func LimitsErrHandler(limit, offset, maxLimit int) error { + maxOffset := maxLimit - limit + if limit > maxLimit { + return errors.New(fmt.Sprintf("InvalidLimitError: Limit specified too high (%d > %d).", limit, maxLimit)) } else if offset > maxOffset { return errors.New(fmt.Sprintf("InvalidOffsetError: Offset specified too high (%d > %d).", offset, maxOffset)) } -- cgit v1.2.3