From 1ede54ddf6dfd2d4ba039eb7e23b74bcac65b643 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Wed, 8 Jun 2022 20:38:03 +0200 Subject: [feature] More consistent API error handling (#637) * update templates * start reworking api error handling * update template * return AP status at web endpoint if negotiated * start making api error handling much more consistent * update account endpoints to new error handling * use new api error handling in admin endpoints * go fmt ./... * use api error logic in app * use generic error handling in auth * don't export generic error handler * don't defer clearing session * user nicer error handling on oidc callback handler * tidy up the sign in handler * tidy up the token handler * use nicer error handling in blocksget * auth emojis endpoint * fix up remaining api endpoints * fix whoopsie during login flow * regenerate swagger docs * change http error logging to debug --- internal/processing/account/create.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'internal/processing/account/create.go') diff --git a/internal/processing/account/create.go b/internal/processing/account/create.go index 72626220b..44d6bbea5 100644 --- a/internal/processing/account/create.go +++ b/internal/processing/account/create.go @@ -27,29 +27,30 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/ap" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/config" + "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/messages" "github.com/superseriousbusiness/gotosocial/internal/text" "github.com/superseriousbusiness/oauth2/v4" ) -func (p *processor) Create(ctx context.Context, applicationToken oauth2.TokenInfo, application *gtsmodel.Application, form *apimodel.AccountCreateRequest) (*apimodel.Token, error) { +func (p *processor) Create(ctx context.Context, applicationToken oauth2.TokenInfo, application *gtsmodel.Application, form *apimodel.AccountCreateRequest) (*apimodel.Token, gtserror.WithCode) { l := logrus.WithField("func", "accountCreate") emailAvailable, err := p.db.IsEmailAvailable(ctx, form.Email) if err != nil { - return nil, err + return nil, gtserror.NewErrorBadRequest(err) } if !emailAvailable { - return nil, fmt.Errorf("email address %s in use", form.Email) + return nil, gtserror.NewErrorConflict(fmt.Errorf("email address %s is not available", form.Email)) } usernameAvailable, err := p.db.IsUsernameAvailable(ctx, form.Username) if err != nil { - return nil, err + return nil, gtserror.NewErrorBadRequest(err) } if !usernameAvailable { - return nil, fmt.Errorf("username %s in use", form.Username) + return nil, gtserror.NewErrorConflict(fmt.Errorf("username %s in use", form.Username)) } reasonRequired := config.GetAccountsReasonRequired() @@ -64,19 +65,19 @@ func (p *processor) Create(ctx context.Context, applicationToken oauth2.TokenInf l.Trace("creating new username and account") user, err := p.db.NewSignup(ctx, form.Username, text.SanitizePlaintext(reason), approvalRequired, form.Email, form.Password, form.IP, form.Locale, application.ID, false, false) if err != nil { - return nil, fmt.Errorf("error creating new signup in the database: %s", err) + return nil, gtserror.NewErrorInternalError(fmt.Errorf("error creating new signup in the database: %s", err)) } l.Tracef("generating a token for user %s with account %s and application %s", user.ID, user.AccountID, application.ID) accessToken, err := p.oauthServer.GenerateUserAccessToken(ctx, applicationToken, application.ClientSecret, user.ID) if err != nil { - return nil, fmt.Errorf("error creating new access token for user %s: %s", user.ID, err) + return nil, gtserror.NewErrorInternalError(fmt.Errorf("error creating new access token for user %s: %s", user.ID, err)) } if user.Account == nil { a, err := p.db.GetAccountByID(ctx, user.AccountID) if err != nil { - return nil, fmt.Errorf("error getting new account from the database: %s", err) + return nil, gtserror.NewErrorInternalError(fmt.Errorf("error getting new account from the database: %s", err)) } user.Account = a } -- cgit v1.2.3