mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[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
This commit is contained in:
@@ -29,6 +29,7 @@ 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/media"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
||||
@@ -37,7 +38,7 @@ import (
|
||||
"github.com/superseriousbusiness/gotosocial/internal/validate"
|
||||
)
|
||||
|
||||
func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, error) {
|
||||
func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, gtserror.WithCode) {
|
||||
l := logrus.WithField("func", "AccountUpdate")
|
||||
|
||||
if form.Discoverable != nil {
|
||||
@@ -50,14 +51,14 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
|
||||
|
||||
if form.DisplayName != nil {
|
||||
if err := validate.DisplayName(*form.DisplayName); err != nil {
|
||||
return nil, err
|
||||
return nil, gtserror.NewErrorBadRequest(err)
|
||||
}
|
||||
account.DisplayName = text.SanitizePlaintext(*form.DisplayName)
|
||||
}
|
||||
|
||||
if form.Note != nil {
|
||||
if err := validate.Note(*form.Note); err != nil {
|
||||
return nil, err
|
||||
return nil, gtserror.NewErrorBadRequest(err)
|
||||
}
|
||||
|
||||
// Set the raw note before processing
|
||||
@@ -66,7 +67,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
|
||||
// Process note to generate a valid HTML representation
|
||||
note, err := p.processNote(ctx, *form.Note, account.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, gtserror.NewErrorBadRequest(err)
|
||||
}
|
||||
|
||||
// Set updated HTML-ified note
|
||||
@@ -76,7 +77,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
|
||||
if form.Avatar != nil && form.Avatar.Size != 0 {
|
||||
avatarInfo, err := p.UpdateAvatar(ctx, form.Avatar, account.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, gtserror.NewErrorBadRequest(err)
|
||||
}
|
||||
account.AvatarMediaAttachmentID = avatarInfo.ID
|
||||
account.AvatarMediaAttachment = avatarInfo
|
||||
@@ -86,7 +87,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
|
||||
if form.Header != nil && form.Header.Size != 0 {
|
||||
headerInfo, err := p.UpdateHeader(ctx, form.Header, account.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, gtserror.NewErrorBadRequest(err)
|
||||
}
|
||||
account.HeaderMediaAttachmentID = headerInfo.ID
|
||||
account.HeaderMediaAttachment = headerInfo
|
||||
@@ -100,7 +101,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
|
||||
if form.Source != nil {
|
||||
if form.Source.Language != nil {
|
||||
if err := validate.Language(*form.Source.Language); err != nil {
|
||||
return nil, err
|
||||
return nil, gtserror.NewErrorBadRequest(err)
|
||||
}
|
||||
account.Language = *form.Source.Language
|
||||
}
|
||||
@@ -111,7 +112,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
|
||||
|
||||
if form.Source.Privacy != nil {
|
||||
if err := validate.Privacy(*form.Source.Privacy); err != nil {
|
||||
return nil, err
|
||||
return nil, gtserror.NewErrorBadRequest(err)
|
||||
}
|
||||
privacy := p.tc.APIVisToVis(apimodel.Visibility(*form.Source.Privacy))
|
||||
account.Privacy = privacy
|
||||
@@ -120,7 +121,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
|
||||
|
||||
updatedAccount, err := p.db.UpdateAccount(ctx, account)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not update account %s: %s", account.ID, err)
|
||||
return nil, gtserror.NewErrorInternalError(fmt.Errorf("could not update account %s: %s", account.ID, err))
|
||||
}
|
||||
|
||||
p.clientWorker.Queue(messages.FromClientAPI{
|
||||
@@ -132,7 +133,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
|
||||
|
||||
acctSensitive, err := p.tc.AccountToAPIAccountSensitive(ctx, updatedAccount)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not convert account into apisensitive account: %s", err)
|
||||
return nil, gtserror.NewErrorInternalError(fmt.Errorf("could not convert account into apisensitive account: %s", err))
|
||||
}
|
||||
return acctSensitive, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user