forgejo/services/federation/error.go
famfo 5f432e32c8 chore(federation): re-enable nilnil lint (#11253)
First round of patches to re-enable some lints from my side.

This PR also refactors the general key fetching code quite a bit due to the way it currently worked
with relying on some values being nil sometimes.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11253
Reviewed-by: elle <0xllx0@noreply.codeberg.org>
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: famfo <famfo@famfo.xyz>
Co-committed-by: famfo <famfo@famfo.xyz>
2026-04-13 22:05:29 +02:00

52 lines
1 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package federation
import (
"fmt"
"net/http"
)
type ErrNotAcceptable struct {
Message string
}
func NewErrNotAcceptablef(format string, a ...any) ErrNotAcceptable {
message := fmt.Sprintf(format, a...)
return ErrNotAcceptable{Message: message}
}
func (err ErrNotAcceptable) Error() string {
return fmt.Sprintf("NotAcceptable: %v", err.Message)
}
type ErrInternal struct {
Message string
}
func NewErrInternalf(format string, a ...any) ErrInternal {
message := fmt.Sprintf(format, a...)
return ErrInternal{Message: message}
}
func (err ErrInternal) Error() string {
return fmt.Sprintf("InternalServerError: %v", err.Message)
}
func HTTPStatus(err error) int {
switch err.(type) {
case ErrNotAcceptable:
return http.StatusNotAcceptable
default:
return http.StatusInternalServerError
}
}
type ErrKeyNotFound struct {
KeyID string
}
func (err ErrKeyNotFound) Error() string {
return fmt.Sprintf("No key found for key ID: %s", err.KeyID)
}