Skip to content

Commit

Permalink
refactor: move errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Apr 25, 2022
1 parent 08c0d1a commit 5f3727a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 42 deletions.
17 changes: 17 additions & 0 deletions core/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core

import "fmt"

var (
ErrPresetNotFound = fmt.Errorf("preset not found")
)

var (
ErrHubDiscovering = fmt.Errorf("hub is discovering")
ErrHubServiceClosed = fmt.Errorf("hub service closed")
)

var (
ErrRadioClosed = fmt.Errorf("radio closed")
ErrRadioNotFound = fmt.Errorf("radio not found")
)
28 changes: 14 additions & 14 deletions core/middleware/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,39 @@ func NewPreset(pub state.MiddlewarePub, presetStore preset.PresetStore) *Preset
}
}

func (m *Preset) List(ctx context.Context) ([]preset.Preset, error) {
return m.store.List(ctx)
func (p *Preset) List(ctx context.Context) ([]preset.Preset, error) {
return p.store.List(ctx)
}

func (m *Preset) Get(ctx context.Context, url string) (*preset.Preset, error) {
return m.store.Get(ctx, url)
func (p *Preset) Get(ctx context.Context, url string) (*preset.Preset, error) {
return p.store.Get(ctx, url)
}

func (m *Preset) Update(ctx context.Context, preset *preset.Preset) error {
err := m.store.Update(ctx, preset)
func (p *Preset) Update(ctx context.Context, preset *preset.Preset) error {
err := p.store.Update(ctx, preset)
if err != nil {
return err
}

m.pub.Publish()
p.pub.Publish()
return nil
}

func (m *Preset) Apply(frag *state.Fragment) {
func (p *Preset) Apply(frag *state.Fragment) {
ctx := context.Background()

m.fragmentPresets(ctx, frag)
p.fragmentPresets(ctx, frag)

m.fragmentTitleAndURL(ctx, frag)
p.fragmentTitleAndURL(ctx, frag)
}

func (m *Preset) fragmentPresets(ctx context.Context, frag *state.Fragment) {
func (p *Preset) fragmentPresets(ctx context.Context, frag *state.Fragment) {
if frag.Presets != nil {
for i := range frag.Presets {
titleNew := ""
urlNew := ""

preset, err := m.store.Get(ctx, frag.Presets[i].URL)
preset, err := p.store.Get(ctx, frag.Presets[i].URL)
if err == nil {
titleNew = preset.TitleNew
urlNew = preset.URLNew
Expand All @@ -63,12 +63,12 @@ func (m *Preset) fragmentPresets(ctx context.Context, frag *state.Fragment) {
}
}

func (m *Preset) fragmentTitleAndURL(ctx context.Context, frag *state.Fragment) {
func (p *Preset) fragmentTitleAndURL(ctx context.Context, frag *state.Fragment) {
if frag.URL != nil {
urlNew := ""
titleNew := ""

preset, err := m.store.Get(ctx, *frag.URL)
preset, err := p.store.Get(ctx, *frag.URL)
if err == nil {
urlNew = preset.URLNew
titleNew = preset.TitleNew
Expand Down
4 changes: 0 additions & 4 deletions core/preset/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import (
"net/url"
)

var (
ErrPresetNotFound = fmt.Errorf("preset not found")
)

type (
Preset struct {
TitleNew string `json:"title_new"`
Expand Down
13 changes: 5 additions & 8 deletions core/radio/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import (
"log"
"sync"

"github.com/ItsNotGoodName/reciva-web-remote/core"
"github.com/ItsNotGoodName/reciva-web-remote/core/upnp"
)

var (
ErrHubDiscovering = fmt.Errorf("hub is discovering")
ErrHubServiceClosed = fmt.Errorf("hub service closed")
ErrRadioNotFound = fmt.Errorf("radio not found")
)
var ()

type HubServiceImpl struct {
discoverC chan chan discoverResponse
Expand Down Expand Up @@ -45,9 +42,9 @@ func (hs *HubServiceImpl) Discover() (int, error) {
res := <-resC
return res.count, res.err
case <-hs.doneC:
return 0, ErrHubServiceClosed
return 0, core.ErrHubServiceClosed
default:
return 0, ErrHubDiscovering
return 0, core.ErrHubDiscovering
}
}

Expand Down Expand Up @@ -137,7 +134,7 @@ func (hs *HubServiceImpl) Get(uuid string) (Radio, error) {
r, ok := hs.radiosMap[uuid]
hs.radioMapMu.RUnlock()
if !ok {
return Radio{}, ErrRadioNotFound
return Radio{}, core.ErrRadioNotFound
}

return r, nil
Expand Down
10 changes: 3 additions & 7 deletions core/radio/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ package radio

import (
"context"
"fmt"

"github.com/ItsNotGoodName/go-upnpsub"
"github.com/ItsNotGoodName/reciva-web-remote/core"
"github.com/ItsNotGoodName/reciva-web-remote/core/state"
"github.com/huin/goupnp"
)

var (
ErrRadioClosed = fmt.Errorf("radio closed")
)

type (
HubService interface {
Discover() (int, error)
Expand Down Expand Up @@ -66,7 +62,7 @@ func (r *Radio) read(ctx context.Context) (*state.State, error) {
case <-ctx.Done():
return nil, ctx.Err()
case <-r.Done():
return nil, ErrRadioClosed
return nil, core.ErrRadioClosed
case state := <-r.readC:
return &state, nil
}
Expand All @@ -77,7 +73,7 @@ func (r *Radio) update(ctx context.Context, frag state.Fragment) error {
case <-ctx.Done():
return ctx.Err()
case <-r.Done():
return ErrRadioClosed
return core.ErrRadioClosed
case r.updateC <- frag:
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion left/api/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"encoding/json"
"net/http"

"github.com/ItsNotGoodName/reciva-web-remote/core"
"github.com/ItsNotGoodName/reciva-web-remote/core/preset"
"github.com/ItsNotGoodName/reciva-web-remote/left/presenter"
)

func handlePresetError(err error) presenter.Response {
code := http.StatusInternalServerError
if err == preset.ErrPresetNotFound {
if err == core.ErrPresetNotFound {
code = http.StatusNotFound
}

Expand Down
9 changes: 5 additions & 4 deletions left/api/radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/http"

"github.com/ItsNotGoodName/reciva-web-remote/core"
"github.com/ItsNotGoodName/reciva-web-remote/core/radio"
"github.com/ItsNotGoodName/reciva-web-remote/core/state"
"github.com/ItsNotGoodName/reciva-web-remote/left/presenter"
Expand All @@ -15,9 +16,9 @@ type RadioRequester func(*http.Request, radio.Radio) presenter.Response

func handleRadioError(err error) presenter.Response {
code := http.StatusInternalServerError
if err == radio.ErrRadioClosed {
if err == core.ErrRadioClosed {
code = http.StatusGone
} else if err == radio.ErrRadioNotFound {
} else if err == core.ErrRadioNotFound {
code = http.StatusNotFound
}

Expand Down Expand Up @@ -69,9 +70,9 @@ func PostRadios(hub radio.HubService) presenter.Requester {
count, err := hub.Discover()
if err != nil {
code := http.StatusInternalServerError
if err == radio.ErrHubDiscovering {
if err == core.ErrHubDiscovering {
code = http.StatusConflict
} else if err == radio.ErrHubServiceClosed {
} else if err == core.ErrHubServiceClosed {
code = http.StatusServiceUnavailable
}
return presenter.Response{
Expand Down
5 changes: 3 additions & 2 deletions right/file/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/fs"
"sync"

"github.com/ItsNotGoodName/reciva-web-remote/core"
"github.com/ItsNotGoodName/reciva-web-remote/core/preset"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func (ps *PresetStore) Get(ctx context.Context, url string) (*preset.Preset, err
ps.presetsMapMu.RUnlock()

if !ok {
return nil, preset.ErrPresetNotFound
return nil, core.ErrPresetNotFound
}

return &p, nil
Expand All @@ -61,7 +62,7 @@ func (ps *PresetStore) Update(ctx context.Context, p *preset.Preset) error {

old, ok := ps.presetsMap[p.URL]
if !ok {
return preset.ErrPresetNotFound
return core.ErrPresetNotFound
}

ps.presetsMap[p.URL] = *p
Expand Down
5 changes: 3 additions & 2 deletions right/mock/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mock
import (
"context"

"github.com/ItsNotGoodName/reciva-web-remote/core"
"github.com/ItsNotGoodName/reciva-web-remote/core/preset"
)

Expand All @@ -17,9 +18,9 @@ func (PresetStore) List(context.Context) ([]preset.Preset, error) {
}

func (PresetStore) Get(context.Context, string) (*preset.Preset, error) {
return nil, preset.ErrPresetNotFound
return nil, core.ErrPresetNotFound
}

func (PresetStore) Update(context.Context, *preset.Preset) error {
return preset.ErrPresetNotFound
return core.ErrPresetNotFound
}

0 comments on commit 5f3727a

Please sign in to comment.