2024-11-26 23:30:58 +00:00
|
|
|
package service
|
2024-11-26 23:28:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-12-05 23:50:02 +00:00
|
|
|
"errors"
|
2024-11-26 23:28:48 +00:00
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/api"
|
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
|
2024-11-29 23:49:25 +00:00
|
|
|
"github.com/nats-io/nats.go"
|
2024-11-26 23:28:48 +00:00
|
|
|
)
|
|
|
|
|
2024-12-05 23:50:02 +00:00
|
|
|
var (
|
|
|
|
// ErrPermissionDenied caller does not have the required role
|
|
|
|
ErrPermissionDenied = errors.New("Permission denied")
|
|
|
|
// ErrUserInfo user info is missing for some reason
|
|
|
|
ErrUserInfo = errors.New("Missing user info")
|
|
|
|
)
|
|
|
|
|
2024-11-26 23:30:58 +00:00
|
|
|
type Service struct {
|
|
|
|
DB datastore.Datastore
|
2024-11-29 23:49:25 +00:00
|
|
|
Nats *nats.Conn
|
2024-11-26 23:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewError creates *ErrorStatusCode from error returned by handler.
|
|
|
|
//
|
|
|
|
// Used for common default response.
|
2024-11-26 23:30:58 +00:00
|
|
|
func (svc *Service) NewError(ctx context.Context, err error) *api.ErrorStatusCode {
|
2024-12-11 05:49:03 +00:00
|
|
|
status := 500
|
|
|
|
if errors.Is(err,ErrPermissionDenied){
|
|
|
|
status = 403
|
|
|
|
}
|
|
|
|
if errors.Is(err,ErrUserInfo){
|
|
|
|
status = 401
|
|
|
|
}
|
2024-11-26 23:28:48 +00:00
|
|
|
return &api.ErrorStatusCode{
|
2024-12-11 05:49:03 +00:00
|
|
|
StatusCode: status,
|
|
|
|
Response: api.Error{
|
|
|
|
Code: int64(status),
|
|
|
|
Message: err.Error(),
|
|
|
|
},
|
2024-11-26 23:28:48 +00:00
|
|
|
}
|
|
|
|
}
|