From 0dd5df030216d8a6418a9f94aa5aa58ad0af86d9 Mon Sep 17 00:00:00 2001 From: Sean Du Date: Sat, 6 Jun 2026 16:41:18 +0800 Subject: [PATCH] Unify fallback and unimplemented API error handling - Add a shared common error response schema and unknown error code for business-level fallbacks - Return business error payloads for router NoRoute and NoMethod fallbacks - Align the existing event list fallback response with the shared error structure - Introduce an explicit unimplemented API error type instead of relying on formatted strings - Make /api batch dispatch return per-item status 600 error entries for unimplemented modules and actions without failing the whole batch Signed-off-by: Sean Du --- internal/constant/errorcode.go | 2 + internal/handler/api/album/album.go | 4 +- internal/handler/api/api.go | 17 ++++++++- internal/handler/api/award/award.go | 4 +- internal/handler/api/background/background.go | 4 +- internal/handler/api/banner/banner.go | 4 +- internal/handler/api/challenge/challenge.go | 4 +- internal/handler/api/costume/costume.go | 4 +- .../api/eventscenario/eventscenario.go | 4 +- internal/handler/api/exchange/exchange.go | 4 +- internal/handler/api/item/item.go | 4 +- internal/handler/api/live/live.go | 4 +- internal/handler/api/liveicon/liveicon.go | 4 +- internal/handler/api/livese/livese.go | 4 +- internal/handler/api/login/login.go | 4 +- internal/handler/api/marathon/marathon.go | 4 +- internal/handler/api/multiunit/multiunit.go | 4 +- internal/handler/api/museum/museum.go | 4 +- internal/handler/api/navigation/navigation.go | 4 +- internal/handler/api/notice/notice.go | 4 +- internal/handler/api/payment/payment.go | 4 +- internal/handler/api/profile/profile.go | 4 +- internal/handler/api/reward/reward.go | 4 +- internal/handler/api/scenario/scenario.go | 4 +- internal/handler/api/secretbox/secretbox.go | 4 +- internal/handler/api/stamp/stamp.go | 4 +- .../handler/api/subscenario/subscenario.go | 4 +- internal/handler/api/unit/unit.go | 4 +- internal/handler/api/user/user.go | 4 +- internal/handler/event/list.go | 18 +++------ internal/router/router.go | 18 +++++++++ internal/schema/common/common.go | 17 ++++++++- internal/utils/error.go | 37 +++++++++++++++++++ internal/utils/request.go | 2 +- internal/utils/serverdata.go | 2 +- 35 files changed, 149 insertions(+), 72 deletions(-) create mode 100644 internal/utils/error.go diff --git a/internal/constant/errorcode.go b/internal/constant/errorcode.go index 533cfa0..41d4d0f 100644 --- a/internal/constant/errorcode.go +++ b/internal/constant/errorcode.go @@ -4,6 +4,8 @@ type ErrorCode int // https://github.com/DarkEnergyProcessor/NPPS4/blob/master/npps4/idol/error.py const ( + ErrorCodeUnknown ErrorCode = 1 ErrorCodeNoUnitIsSellable ErrorCode = 1205 ErrorCodeLivePreciseListNotFound ErrorCode = 3421 + ErrorCodeEventNoEventData ErrorCode = 10004 ) diff --git a/internal/handler/api/album/album.go b/internal/handler/api/album/album.go index 3cd8eac..bdd30df 100644 --- a/internal/handler/api/album/album.go +++ b/internal/handler/api/album/album.go @@ -1,7 +1,7 @@ package album import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func AlbumApi(ctx *gin.Context, action string) (res any, err error) { case "albumAll": res, err = albumAll(ctx) default: - err = fmt.Errorf("unimplemented action: album: %s", action) + err = honokautils.NewUnimplementedActionError("album", action) } return res, err } diff --git a/internal/handler/api/api.go b/internal/handler/api/api.go index 53d1d9b..624300d 100644 --- a/internal/handler/api/api.go +++ b/internal/handler/api/api.go @@ -1,7 +1,7 @@ package api import ( - "fmt" + "honoka-chan/internal/constant" "honoka-chan/internal/handler/api/album" "honoka-chan/internal/handler/api/award" "honoka-chan/internal/handler/api/background" @@ -32,8 +32,10 @@ import ( "honoka-chan/internal/middleware" "honoka-chan/internal/router" apischema "honoka-chan/internal/schema/api" + commonschema "honoka-chan/internal/schema/common" "honoka-chan/internal/session" honokautils "honoka-chan/internal/utils" + "time" "github.com/gin-gonic/gin" ) @@ -108,9 +110,20 @@ func api(ctx *gin.Context) { case "user": result, err = user.UserApi(ctx, v.Action) default: - err = fmt.Errorf("unimplemented api module: %s", v.Module) + err = honokautils.NewUnimplementedModuleError(v.Module) } + if honokautils.IsUnimplementedError(err) { + results = append(results, commonschema.ApiErrorResp{ + Result: commonschema.ErrorData{ + ErrorCode: constant.ErrorCodeUnknown, + }, + Status: 600, + CommandNum: false, + TimeStamp: time.Now().Unix(), + }) + continue + } if ss.CheckErr(err) { return } diff --git a/internal/handler/api/award/award.go b/internal/handler/api/award/award.go index 42e65d4..cab2790 100644 --- a/internal/handler/api/award/award.go +++ b/internal/handler/api/award/award.go @@ -1,7 +1,7 @@ package award import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func AwardApi(ctx *gin.Context, action string) (res any, err error) { case "awardInfo": res, err = awardInfo(ctx) default: - err = fmt.Errorf("unimplemented action: award: %s", action) + err = honokautils.NewUnimplementedActionError("award", action) } return res, err } diff --git a/internal/handler/api/background/background.go b/internal/handler/api/background/background.go index 371020c..08fc357 100644 --- a/internal/handler/api/background/background.go +++ b/internal/handler/api/background/background.go @@ -1,7 +1,7 @@ package background import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func BackgroundApi(ctx *gin.Context, action string) (res any, err error) { case "backgroundInfo": res, err = backgroundInfo(ctx) default: - err = fmt.Errorf("unimplemented action: background: %s", action) + err = honokautils.NewUnimplementedActionError("background", action) } return res, err } diff --git a/internal/handler/api/banner/banner.go b/internal/handler/api/banner/banner.go index 08900ec..5060db9 100644 --- a/internal/handler/api/banner/banner.go +++ b/internal/handler/api/banner/banner.go @@ -1,13 +1,13 @@ package banner -import "fmt" +import honokautils "honoka-chan/internal/utils" func BannerApi(action string) (res any, err error) { switch action { case "bannerList": res, err = bannerList() default: - err = fmt.Errorf("unimplemented action: banner: %s", action) + err = honokautils.NewUnimplementedActionError("banner", action) } return res, err } diff --git a/internal/handler/api/challenge/challenge.go b/internal/handler/api/challenge/challenge.go index 24f0828..78a4b75 100644 --- a/internal/handler/api/challenge/challenge.go +++ b/internal/handler/api/challenge/challenge.go @@ -1,7 +1,7 @@ package challenge import ( - "fmt" + honokautils "honoka-chan/internal/utils" ) func ChallengeApi(action string) (res any, err error) { @@ -9,7 +9,7 @@ func ChallengeApi(action string) (res any, err error) { case "challengeInfo": res, err = challengeInfo() default: - err = fmt.Errorf("unimplemented action: challenge: %s", action) + err = honokautils.NewUnimplementedActionError("challenge", action) } return res, err } diff --git a/internal/handler/api/costume/costume.go b/internal/handler/api/costume/costume.go index 63885eb..686ee05 100644 --- a/internal/handler/api/costume/costume.go +++ b/internal/handler/api/costume/costume.go @@ -1,13 +1,13 @@ package costume -import "fmt" +import honokautils "honoka-chan/internal/utils" func CostumeApi(action string) (res any, err error) { switch action { case "costumeList": res, err = costumeList() default: - err = fmt.Errorf("unimplemented action: costume: %s", action) + err = honokautils.NewUnimplementedActionError("costume", action) } return res, err } diff --git a/internal/handler/api/eventscenario/eventscenario.go b/internal/handler/api/eventscenario/eventscenario.go index f690b17..55f0eb8 100644 --- a/internal/handler/api/eventscenario/eventscenario.go +++ b/internal/handler/api/eventscenario/eventscenario.go @@ -1,7 +1,7 @@ package eventscenario import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func EventScenarioApi(ctx *gin.Context, action string) (res any, err error) { case "status": res, err = eventScenarioStatus(ctx) default: - err = fmt.Errorf("unimplemented action: costume: %s", action) + err = honokautils.NewUnimplementedActionError("eventscenario", action) } return res, err } diff --git a/internal/handler/api/exchange/exchange.go b/internal/handler/api/exchange/exchange.go index d262aea..f7564ad 100644 --- a/internal/handler/api/exchange/exchange.go +++ b/internal/handler/api/exchange/exchange.go @@ -1,7 +1,7 @@ package exchange import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func ExchangeApi(ctx *gin.Context, action string) (res any, err error) { case "owningPoint": res, err = owningPoint(ctx) default: - err = fmt.Errorf("unimplemented action: exchange: %s", action) + err = honokautils.NewUnimplementedActionError("exchange", action) } return res, err } diff --git a/internal/handler/api/item/item.go b/internal/handler/api/item/item.go index d23065b..1f443ed 100644 --- a/internal/handler/api/item/item.go +++ b/internal/handler/api/item/item.go @@ -1,13 +1,13 @@ package item -import "fmt" +import honokautils "honoka-chan/internal/utils" func ItemApi(action string) (res any, err error) { switch action { case "list": res, err = itemList() default: - err = fmt.Errorf("unimplemented action: item: %s", action) + err = honokautils.NewUnimplementedActionError("item", action) } return res, err } diff --git a/internal/handler/api/live/live.go b/internal/handler/api/live/live.go index f06b872..4cfad92 100644 --- a/internal/handler/api/live/live.go +++ b/internal/handler/api/live/live.go @@ -1,7 +1,7 @@ package live import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -13,7 +13,7 @@ func LiveApi(ctx *gin.Context, action string) (res any, err error) { case "schedule": res, err = liveSchedule(ctx) default: - err = fmt.Errorf("unimplemented action: live: %s", action) + err = honokautils.NewUnimplementedActionError("live", action) } return res, err } diff --git a/internal/handler/api/liveicon/liveicon.go b/internal/handler/api/liveicon/liveicon.go index 33bf93a..b879144 100644 --- a/internal/handler/api/liveicon/liveicon.go +++ b/internal/handler/api/liveicon/liveicon.go @@ -1,13 +1,13 @@ package liveicon -import "fmt" +import honokautils "honoka-chan/internal/utils" func LiveIconApi(action string) (res any, err error) { switch action { case "liveiconInfo": res, err = liveIconInfo() default: - err = fmt.Errorf("unimplemented action: liveicon: %s", action) + err = honokautils.NewUnimplementedActionError("liveicon", action) } return res, err } diff --git a/internal/handler/api/livese/livese.go b/internal/handler/api/livese/livese.go index f681a9d..87a4fd7 100644 --- a/internal/handler/api/livese/livese.go +++ b/internal/handler/api/livese/livese.go @@ -1,13 +1,13 @@ package livese -import "fmt" +import honokautils "honoka-chan/internal/utils" func LiveSeApi(action string) (res any, err error) { switch action { case "liveseInfo": res, err = LiveSeInfo() default: - err = fmt.Errorf("unimplemented action: livese: %s", action) + err = honokautils.NewUnimplementedActionError("livese", action) } return res, err } diff --git a/internal/handler/api/login/login.go b/internal/handler/api/login/login.go index 1ac476a..fcabc1c 100644 --- a/internal/handler/api/login/login.go +++ b/internal/handler/api/login/login.go @@ -1,7 +1,7 @@ package login import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -13,7 +13,7 @@ func LoginApi(ctx *gin.Context, action string) (res any, err error) { case "topInfoOnce": res, err = loginTopInfoOnce(ctx) default: - err = fmt.Errorf("unimplemented action: login: %s", action) + err = honokautils.NewUnimplementedActionError("login", action) } return res, err } diff --git a/internal/handler/api/marathon/marathon.go b/internal/handler/api/marathon/marathon.go index 69f9939..9599ec7 100644 --- a/internal/handler/api/marathon/marathon.go +++ b/internal/handler/api/marathon/marathon.go @@ -1,13 +1,13 @@ package marathon -import "fmt" +import honokautils "honoka-chan/internal/utils" func MarathonApi(action string) (res any, err error) { switch action { case "marathonInfo": res, err = marathonInfo() default: - err = fmt.Errorf("unimplemented action: marathon: %s", action) + err = honokautils.NewUnimplementedActionError("marathon", action) } return res, err } diff --git a/internal/handler/api/multiunit/multiunit.go b/internal/handler/api/multiunit/multiunit.go index 349b80d..0f1fe1f 100644 --- a/internal/handler/api/multiunit/multiunit.go +++ b/internal/handler/api/multiunit/multiunit.go @@ -1,7 +1,7 @@ package multiunit import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func MultiUnitApi(ctx *gin.Context, action string) (res any, err error) { case "multiunitscenarioStatus": res, err = MultiUnitScenarioStatus(ctx) default: - err = fmt.Errorf("unimplemented action: multiunit: %s", action) + err = honokautils.NewUnimplementedActionError("multiunit", action) } return res, err } diff --git a/internal/handler/api/museum/museum.go b/internal/handler/api/museum/museum.go index 53f436e..48e8ac4 100644 --- a/internal/handler/api/museum/museum.go +++ b/internal/handler/api/museum/museum.go @@ -1,7 +1,7 @@ package museum import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func MuseumApi(ctx *gin.Context, action string) (res any, err error) { case "info": res, err = museumInfo(ctx) default: - err = fmt.Errorf("unimplemented action: museum: %s", action) + err = honokautils.NewUnimplementedActionError("museum", action) } return res, err } diff --git a/internal/handler/api/navigation/navigation.go b/internal/handler/api/navigation/navigation.go index 64d9767..04db943 100644 --- a/internal/handler/api/navigation/navigation.go +++ b/internal/handler/api/navigation/navigation.go @@ -1,7 +1,7 @@ package navigation import ( - "fmt" + honokautils "honoka-chan/internal/utils" ) func NavigationApi(action string) (res any, err error) { @@ -9,7 +9,7 @@ func NavigationApi(action string) (res any, err error) { case "specialCutin": res, err = SpecialCutin() default: - err = fmt.Errorf("unimplemented action: navigation: %s", action) + err = honokautils.NewUnimplementedActionError("navigation", action) } return res, err } diff --git a/internal/handler/api/notice/notice.go b/internal/handler/api/notice/notice.go index 327fa3f..7cc4998 100644 --- a/internal/handler/api/notice/notice.go +++ b/internal/handler/api/notice/notice.go @@ -1,13 +1,13 @@ package notice -import "fmt" +import honokautils "honoka-chan/internal/utils" func NoticeApi(action string) (res any, err error) { switch action { case "noticeMarquee": res, err = noticeMarquee() default: - err = fmt.Errorf("unimplemented action: notice: %s", action) + err = honokautils.NewUnimplementedActionError("notice", action) } return res, err } diff --git a/internal/handler/api/payment/payment.go b/internal/handler/api/payment/payment.go index 62b956a..be73f66 100644 --- a/internal/handler/api/payment/payment.go +++ b/internal/handler/api/payment/payment.go @@ -1,7 +1,7 @@ package payment import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func PaymentApi(ctx *gin.Context, action string) (res any, err error) { case "productList": res, err = productList(ctx) default: - err = fmt.Errorf("unimplemented action: payment: %s", action) + err = honokautils.NewUnimplementedActionError("payment", action) } return res, err } diff --git a/internal/handler/api/profile/profile.go b/internal/handler/api/profile/profile.go index 7c53804..bfd2f3c 100644 --- a/internal/handler/api/profile/profile.go +++ b/internal/handler/api/profile/profile.go @@ -1,7 +1,7 @@ package profile import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -15,7 +15,7 @@ func ProfileApi(ctx *gin.Context, action string, targetUserID int) (res any, err case "profileInfo": res, err = profileInfo(ctx, targetUserID) default: - err = fmt.Errorf("unimplemented action: profile: %s", action) + err = honokautils.NewUnimplementedActionError("profile", action) } return res, err } diff --git a/internal/handler/api/reward/reward.go b/internal/handler/api/reward/reward.go index 297b020..1385389 100644 --- a/internal/handler/api/reward/reward.go +++ b/internal/handler/api/reward/reward.go @@ -1,7 +1,7 @@ package reward import ( - "fmt" + honokautils "honoka-chan/internal/utils" ) func RewardApi(action string) (res any, err error) { @@ -11,7 +11,7 @@ func RewardApi(action string) (res any, err error) { case "rewardHistory": res, err = rewardHistory() default: - err = fmt.Errorf("unimplemented action: reward: %s", action) + err = honokautils.NewUnimplementedActionError("reward", action) } return res, err } diff --git a/internal/handler/api/scenario/scenario.go b/internal/handler/api/scenario/scenario.go index 894cd38..a32a42a 100644 --- a/internal/handler/api/scenario/scenario.go +++ b/internal/handler/api/scenario/scenario.go @@ -1,7 +1,7 @@ package scenario import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func ScenarioApi(ctx *gin.Context, action string) (res any, err error) { case "scenarioStatus": res, err = scenarioStatus(ctx) default: - err = fmt.Errorf("unimplemented action: scenario: %s", action) + err = honokautils.NewUnimplementedActionError("scenario", action) } return res, err } diff --git a/internal/handler/api/secretbox/secretbox.go b/internal/handler/api/secretbox/secretbox.go index ac98080..4306f12 100644 --- a/internal/handler/api/secretbox/secretbox.go +++ b/internal/handler/api/secretbox/secretbox.go @@ -1,7 +1,7 @@ package secretbox import ( - "fmt" + honokautils "honoka-chan/internal/utils" ) func SecretBoxApi(action string) (res any, err error) { @@ -9,7 +9,7 @@ func SecretBoxApi(action string) (res any, err error) { case "all": res, err = all() default: - err = fmt.Errorf("unimplemented action: secretbox: %s", action) + err = honokautils.NewUnimplementedActionError("secretbox", action) } return res, err } diff --git a/internal/handler/api/stamp/stamp.go b/internal/handler/api/stamp/stamp.go index b971070..997d37a 100644 --- a/internal/handler/api/stamp/stamp.go +++ b/internal/handler/api/stamp/stamp.go @@ -1,13 +1,13 @@ package stamp -import "fmt" +import honokautils "honoka-chan/internal/utils" func StampApi(action string) (res any, err error) { switch action { case "stampInfo": res, err = stampInfo() default: - err = fmt.Errorf("unimplemented action: stamp: %s", action) + err = honokautils.NewUnimplementedActionError("stamp", action) } return res, err } diff --git a/internal/handler/api/subscenario/subscenario.go b/internal/handler/api/subscenario/subscenario.go index c69347c..14630a3 100644 --- a/internal/handler/api/subscenario/subscenario.go +++ b/internal/handler/api/subscenario/subscenario.go @@ -1,7 +1,7 @@ package subscenario import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func SubscenarioApi(ctx *gin.Context, action string) (res any, err error) { case "subscenarioStatus": res, err = subscenarioStatus(ctx) default: - err = fmt.Errorf("unimplemented action: subscenario: %s", action) + err = honokautils.NewUnimplementedActionError("subscenario", action) } return res, err } diff --git a/internal/handler/api/unit/unit.go b/internal/handler/api/unit/unit.go index e282743..f72a7ce 100644 --- a/internal/handler/api/unit/unit.go +++ b/internal/handler/api/unit/unit.go @@ -1,7 +1,7 @@ package unit import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -23,7 +23,7 @@ func UnitApi(ctx *gin.Context, action string) (res any, err error) { case "unitAll": res, err = unitAll(ctx) default: - err = fmt.Errorf("unimplemented action: unit: %s", action) + err = honokautils.NewUnimplementedActionError("unit", action) } return res, err } diff --git a/internal/handler/api/user/user.go b/internal/handler/api/user/user.go index f2cddc1..97e1fd5 100644 --- a/internal/handler/api/user/user.go +++ b/internal/handler/api/user/user.go @@ -1,7 +1,7 @@ package user import ( - "fmt" + honokautils "honoka-chan/internal/utils" "github.com/gin-gonic/gin" ) @@ -13,7 +13,7 @@ func UserApi(ctx *gin.Context, action string) (res any, err error) { case "userInfo": res, err = userInfo(ctx) default: - err = fmt.Errorf("unimplemented action: user: %s", action) + err = honokautils.NewUnimplementedActionError("user", action) } return res, err } diff --git a/internal/handler/event/list.go b/internal/handler/event/list.go index 137151c..bf55a68 100644 --- a/internal/handler/event/list.go +++ b/internal/handler/event/list.go @@ -1,11 +1,12 @@ package event import ( + "honoka-chan/internal/constant" "honoka-chan/internal/middleware" "honoka-chan/internal/router" + commonschema "honoka-chan/internal/schema/common" eventschema "honoka-chan/internal/schema/event" "honoka-chan/internal/session" - "time" "github.com/gin-gonic/gin" ) @@ -22,21 +23,12 @@ func list(ctx *gin.Context) { }) } - // ss.Respond(event.ListResp{ - // ResponseData: map[string]int{ - // "error_code": 10004, - // }, - // ReleaseInfo: []any{}, - // StatusCode: 600, - // }) - ss.Respond(eventschema.ListResp{ - ResponseData: eventschema.ListData{ - TargetList: targets, - ServerTimestamp: time.Now().Unix(), + ResponseData: commonschema.ErrorData{ + ErrorCode: constant.ErrorCodeEventNoEventData, }, ReleaseInfo: []any{}, - StatusCode: 200, + StatusCode: 600, }) } diff --git a/internal/router/router.go b/internal/router/router.go index 7a4c37d..96ffbd2 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -2,6 +2,9 @@ package router import ( + "honoka-chan/internal/constant" + commonschema "honoka-chan/internal/schema/common" + "honoka-chan/internal/session" "net/http" "os" "path/filepath" @@ -104,6 +107,21 @@ func SifRouter(r *gin.Engine) { ctx.HTML(http.StatusOK, "common/manga.html", gin.H{}) }) + notFoundHandler := func(ctx *gin.Context) { + ss := session.Attach(ctx) + defer ss.Finalize() + + ss.Respond(commonschema.ErrorResp{ + ResponseData: commonschema.ErrorData{ + ErrorCode: constant.ErrorCodeUnknown, + }, + ReleaseInfo: []any{}, + StatusCode: 600, + }) + } + r.NoRoute(notFoundHandler) + r.NoMethod(notFoundHandler) + // router for groupPath, groupInfo := range groups { groupApi := r.Group(groupPath, groupInfo.InitialHandlers...) diff --git a/internal/schema/common/common.go b/internal/schema/common/common.go index 0ee83aa..cbb6a28 100644 --- a/internal/schema/common/common.go +++ b/internal/schema/common/common.go @@ -1,7 +1,22 @@ package commonschema -import "honoka-chan/internal/constant" +import ( + "honoka-chan/internal/constant" +) type ErrorData struct { ErrorCode constant.ErrorCode `json:"error_code"` } + +type ErrorResp struct { + ResponseData ErrorData `json:"response_data"` + ReleaseInfo []any `json:"release_info"` + StatusCode int `json:"status_code"` +} + +type ApiErrorResp struct { + Result ErrorData `json:"result"` + Status int `json:"status"` + CommandNum bool `json:"commandNum"` + TimeStamp int64 `json:"timeStamp"` +} diff --git a/internal/utils/error.go b/internal/utils/error.go new file mode 100644 index 0000000..bca00f0 --- /dev/null +++ b/internal/utils/error.go @@ -0,0 +1,37 @@ +package honokautils + +import "errors" + +type UnimplementedError struct { + Kind string + Module string + Name string +} + +func (e *UnimplementedError) Error() string { + if e == nil { + return "unimplemented" + } + return "unimplemented " + e.Kind + ": " + e.Module + ": " + e.Name +} + +func NewUnimplementedModuleError(module string) error { + return &UnimplementedError{ + Kind: "api module", + Module: module, + Name: module, + } +} + +func NewUnimplementedActionError(module string, action string) error { + return &UnimplementedError{ + Kind: "action", + Module: module, + Name: action, + } +} + +func IsUnimplementedError(err error) bool { + var target *UnimplementedError + return err != nil && errors.As(err, &target) +} diff --git a/internal/utils/request.go b/internal/utils/request.go index 7e4da10..6595aec 100644 --- a/internal/utils/request.go +++ b/internal/utils/request.go @@ -1,4 +1,4 @@ -package utils +package honokautils import ( "encoding/json" diff --git a/internal/utils/serverdata.go b/internal/utils/serverdata.go index 27db73e..8a508e4 100644 --- a/internal/utils/serverdata.go +++ b/internal/utils/serverdata.go @@ -1,4 +1,4 @@ -package utils +package honokautils import ( "encoding/json"