From b5355233fd90ec1539f285834b7f2e0864e57595 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Mon, 17 Jul 2023 15:37:53 +0000 Subject: [PATCH] Generate error enum types (#161) * Generate error enum types * Remove unnecessary newline * Catch errors not covered by LemmyErrorType --------- Co-authored-by: Dessalines --- copy_generated_types_from_lemmy.sh | 1 + src/http.ts | 9 +- src/index.ts | 1 + src/types/LemmyErrorType.ts | 139 +++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/types/LemmyErrorType.ts diff --git a/copy_generated_types_from_lemmy.sh b/copy_generated_types_from_lemmy.sh index 1acc1ab..58e4091 100755 --- a/copy_generated_types_from_lemmy.sh +++ b/copy_generated_types_from_lemmy.sh @@ -12,6 +12,7 @@ cp db_schema/bindings/* ../../lemmy-js-client/src/types/ cp db_views/bindings/* ../../lemmy-js-client/src/types/ cp db_views_actor/bindings/* ../../lemmy-js-client/src/types/ cp db_views_moderator/bindings/* ../../lemmy-js-client/src/types/ +cp utils/bindings/* ../../lemmy-js-client/src/types/ popd diff --git a/src/http.ts b/src/http.ts index 50aa84e..b5e5640 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1329,7 +1329,14 @@ export class LemmyHttp { body: JSON.stringify(form), }); } - const json = await response.json(); + + let json: any | undefined = undefined; + + try { + json = await response.json(); + } catch (error) { + throw response.statusText; + } if (!response.ok) { throw json["error"] ?? response.statusText; diff --git a/src/index.ts b/src/index.ts index 0f6a5ec..c8f4c38 100644 --- a/src/index.ts +++ b/src/index.ts @@ -121,6 +121,7 @@ export { InstanceId } from "./types/InstanceId"; export { Language } from "./types/Language"; export { LanguageId } from "./types/LanguageId"; export { LeaveAdmin } from "./types/LeaveAdmin"; +export { LemmyErrorType } from "./types/LemmyErrorType"; export { ListCommentReports } from "./types/ListCommentReports"; export { ListCommentReportsResponse } from "./types/ListCommentReportsResponse"; export { ListCommunities } from "./types/ListCommunities"; diff --git a/src/types/LemmyErrorType.ts b/src/types/LemmyErrorType.ts new file mode 100644 index 0000000..53baea7 --- /dev/null +++ b/src/types/LemmyErrorType.ts @@ -0,0 +1,139 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type LemmyErrorType = + | { error_type: "ReportReasonRequired" } + | { error_type: "ReportTooLong" } + | { error_type: "NotAModerator" } + | { error_type: "NotAnAdmin" } + | { error_type: "CannotBlockYourself" } + | { error_type: "CannotBlockAdmin" } + | { error_type: "CouldNotUpdateUser" } + | { error_type: "PasswordsDoNotMatch" } + | { error_type: "PasswordIncorrect" } + | { error_type: "EmailNotVerified" } + | { error_type: "EmailRequired" } + | { error_type: "CouldNotUpdateComment" } + | { error_type: "CouldNotUpdatePrivateMessage" } + | { error_type: "CannotLeaveAdmin" } + | { error_type: "NoLinesInHtml" } + | { error_type: "SiteMetadataPageIsNotDoctypeHtml" } + | { error_type: "PictrsResponseError"; message: string } + | { error_type: "PictrsPurgeResponseError"; message: string } + | { error_type: "ImageUrlMissingPathSegments" } + | { error_type: "ImageUrlMissingLastPathSegment" } + | { error_type: "PictrsApiKeyNotProvided" } + | { error_type: "NoContentTypeHeader" } + | { error_type: "NotAnImageType" } + | { error_type: "NotAModOrAdmin" } + | { error_type: "NoAdmins" } + | { error_type: "NotTopAdmin" } + | { error_type: "NotTopMod" } + | { error_type: "NotLoggedIn" } + | { error_type: "SiteBan" } + | { error_type: "Deleted" } + | { error_type: "BannedFromCommunity" } + | { error_type: "CouldNotFindCommunity" } + | { error_type: "PersonIsBlocked" } + | { error_type: "DownvotesAreDisabled" } + | { error_type: "InstanceIsPrivate" } + | { error_type: "InvalidPassword" } + | { error_type: "SiteDescriptionLengthOverflow" } + | { error_type: "HoneypotFailed" } + | { error_type: "RegistrationApplicationIsPending" } + | { error_type: "PrivateInstanceCannotHaveFederationEnabled" } + | { error_type: "Locked" } + | { error_type: "CouldNotCreateComment" } + | { error_type: "MaxCommentDepthReached" } + | { error_type: "EditCommentNotAllowed" } + | { error_type: "OnlyAdminsCanCreateCommunities" } + | { error_type: "CommunityAlreadyExists" } + | { error_type: "LanguageNotAllowed" } + | { error_type: "OnlyModsCanPostInCommunity" } + | { error_type: "CouldNotUpdatePost" } + | { error_type: "EditPostNotAllowed" } + | { error_type: "CouldNotFindPost" } + | { error_type: "EditPrivateMessageNotAllowed" } + | { error_type: "SiteAlreadyExists" } + | { error_type: "ApplicationQuestionRequired" } + | { error_type: "InvalidDefaultPostListingType" } + | { error_type: "RegistrationClosed" } + | { error_type: "RegistrationApplicationAnswerRequired" } + | { error_type: "EmailAlreadyExists" } + | { error_type: "FederationError"; message: string } + | { error_type: "FederationForbiddenByStrictAllowList" } + | { error_type: "PersonIsBannedFromCommunity" } + | { error_type: "ObjectIsNotPublic" } + | { error_type: "InvalidCommunity" } + | { error_type: "CannotCreatePostOrCommentInDeletedOrRemovedCommunity" } + | { error_type: "CannotReceivePage" } + | { error_type: "NewPostCannotBeLocked" } + | { error_type: "OnlyLocalAdminCanRemoveCommunity" } + | { error_type: "OnlyLocalAdminCanRestoreCommunity" } + | { error_type: "NoIdGiven" } + | { error_type: "CouldNotFindUsernameOrEmail" } + | { error_type: "InvalidQuery" } + | { error_type: "ObjectNotLocal" } + | { error_type: "PostIsLocked" } + | { error_type: "PersonIsBannedFromSite" } + | { error_type: "InvalidVoteValue" } + | { error_type: "PageDoesNotSpecifyCreator" } + | { error_type: "PageDoesNotSpecifyGroup" } + | { error_type: "NoCommunityFoundInCC" } + | { error_type: "NoEmailSetup" } + | { error_type: "EmailSmtpServerNeedsAPort" } + | { error_type: "MissingAnEmail" } + | { error_type: "RateLimitError" } + | { error_type: "InvalidName" } + | { error_type: "InvalidDisplayName" } + | { error_type: "InvalidMatrixId" } + | { error_type: "InvalidPostTitle" } + | { error_type: "InvalidBodyField" } + | { error_type: "BioLengthOverflow" } + | { error_type: "MissingTotpToken" } + | { error_type: "IncorrectTotpToken" } + | { error_type: "CouldNotParseTotpSecret" } + | { error_type: "CouldNotLikeComment" } + | { error_type: "CouldNotSaveComment" } + | { error_type: "CouldNotCreateReport" } + | { error_type: "CouldNotResolveReport" } + | { error_type: "CommunityModeratorAlreadyExists" } + | { error_type: "CommunityUserIsAlreadyBanned" } + | { error_type: "CommunityBlockAlreadyExists" } + | { error_type: "CommunityFollowerAlreadyExists" } + | { error_type: "CouldNotUpdateCommunityHiddenStatus" } + | { error_type: "PersonBlockAlreadyExists" } + | { error_type: "UserAlreadyExists" } + | { error_type: "TokenNotFound" } + | { error_type: "CouldNotLikePost" } + | { error_type: "CouldNotSavePost" } + | { error_type: "CouldNotMarkPostAsRead" } + | { error_type: "CouldNotUpdateCommunity" } + | { error_type: "CouldNotUpdateReplies" } + | { error_type: "CouldNotUpdatePersonMentions" } + | { error_type: "PostTitleTooLong" } + | { error_type: "CouldNotCreatePost" } + | { error_type: "CouldNotCreatePrivateMessage" } + | { error_type: "CouldNotUpdatePrivate" } + | { error_type: "SystemErrLogin" } + | { error_type: "CouldNotSetAllRegistrationsAccepted" } + | { error_type: "CouldNotSetAllEmailVerified" } + | { error_type: "Banned" } + | { error_type: "CouldNotGetComments" } + | { error_type: "CouldNotGetPosts" } + | { error_type: "InvalidUrl" } + | { error_type: "EmailSendFailed" } + | { error_type: "Slurs" } + | { error_type: "CouldNotGenerateTotp" } + | { error_type: "CouldNotFindObject" } + | { error_type: "RegistrationDenied"; message: string } + | { error_type: "FederationDisabled" } + | { error_type: "DomainBlocked" } + | { error_type: "DomainNotInAllowList" } + | { error_type: "FederationDisabledByStrictAllowList" } + | { error_type: "SiteNameRequired" } + | { error_type: "SiteNameLengthOverflow" } + | { error_type: "PermissiveRegex" } + | { error_type: "InvalidRegex" } + | { error_type: "CaptchaIncorrect" } + | { error_type: "PasswordResetLimitReached" } + | { error_type: "CouldNotCreateAudioCaptcha" };