Skip to content

Commit

Permalink
fix: remove generic failure type (#12)
Browse files Browse the repository at this point in the history
resolves #5
  • Loading branch information
alanshaw committed Oct 22, 2024
1 parent f6e0582 commit 6f51c2e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 48 deletions.
30 changes: 0 additions & 30 deletions pkg/capability/failure.go

This file was deleted.

58 changes: 58 additions & 0 deletions pkg/service/storage/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package storage

import (
"fmt"

"github.com/storacha/go-ucanto/ucan"
)

type UnsupportedCapabilityError[C any] struct {
capability ucan.Capability[C]
}

func (ue UnsupportedCapabilityError[C]) Name() string {
return "UnsupportedCapability"
}

func (ue UnsupportedCapabilityError[C]) Capability() ucan.Capability[C] {
return ue.capability
}

func (ue UnsupportedCapabilityError[C]) Error() string {
return fmt.Sprintf(`%s does not have a "%s" capability provider`, ue.capability.With(), ue.capability.Can())
}

func NewUnsupportedCapabilityError[C any](capability ucan.Capability[C]) UnsupportedCapabilityError[C] {
return UnsupportedCapabilityError[C]{capability}
}

type BlobSizeLimitExceededError struct {
size uint64
max uint64
}

func (be BlobSizeLimitExceededError) Name() string {
return "BlobSizeOutsideOfSupportedRange"
}

func (be BlobSizeLimitExceededError) Error() string {
return fmt.Sprintf("Blob of %d bytes, exceeds size limit of %d bytes", be.size, be.max)
}

func NewBlobSizeLimitExceededError(size uint64, max uint64) BlobSizeLimitExceededError {
return BlobSizeLimitExceededError{size, max}
}

type AllocatedMemoryNotWrittenError struct{}

func (ae AllocatedMemoryNotWrittenError) Name() string {
return "AllocatedMemoryHadNotBeenWrittenTo"
}

func (ae AllocatedMemoryNotWrittenError) Error() string {
return "Blob not found"
}

func NewAllocatedMemoryNotWrittenError() AllocatedMemoryNotWrittenError {
return AllocatedMemoryNotWrittenError{}
}
22 changes: 4 additions & 18 deletions pkg/service/storage/ucan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package storage
import (
"context"
"errors"
"fmt"
"net/url"
"time"

Expand All @@ -16,7 +15,6 @@ import (
"github.com/storacha/go-ucanto/core/result/failure"
"github.com/storacha/go-ucanto/server"
"github.com/storacha/go-ucanto/ucan"
"github.com/storacha/storage/pkg/capability"
"github.com/storacha/storage/pkg/internal/digestutil"
"github.com/storacha/storage/pkg/store"
"github.com/storacha/storage/pkg/store/allocationstore/allocation"
Expand All @@ -41,10 +39,7 @@ func NewUCANServer(storageService Service) (server.ServerView, error) {

// only service principal can perform an allocation
if cap.With() != iCtx.ID().DID().String() {
return blob.AllocateOk{}, nil, capability.Failure{
Name: "UnsupportedCapability",
Message: fmt.Sprintf(`%s does not have a "%s" capability provider`, cap.With(), cap.Can()),
}
return blob.AllocateOk{}, nil, NewUnsupportedCapabilityError(cap)
}

// check if we already have an allcoation for the blob in this space
Expand All @@ -70,10 +65,7 @@ func NewUCANServer(storageService Service) (server.ServerView, error) {
}

if cap.Nb().Blob.Size > maxUploadSize {
return blob.AllocateOk{}, nil, capability.Failure{
Name: "BlobSizeOutsideOfSupportedRange",
Message: fmt.Sprintf("Blob of %d bytes, exceeds size limit of %d bytes", cap.Nb().Blob.Size, maxUploadSize),
}
return blob.AllocateOk{}, nil, NewBlobSizeLimitExceededError(cap.Nb().Blob.Size, maxUploadSize)
}

expiresIn := uint64(60 * 60 * 24) // 1 day
Expand Down Expand Up @@ -118,19 +110,13 @@ func NewUCANServer(storageService Service) (server.ServerView, error) {

// only service principal can perform an allocation
if cap.With() != iCtx.ID().DID().String() {
return blob.AcceptOk{}, nil, capability.Failure{
Name: "UnsupportedCapability",
Message: fmt.Sprintf(`%s does not have a "%s" capability provider`, cap.With(), cap.Can()),
}
return blob.AcceptOk{}, nil, NewUnsupportedCapabilityError(cap)
}

_, err := storageService.Blobs().Store().Get(ctx, digest)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
return blob.AcceptOk{}, nil, capability.Failure{
Name: "AllocatedMemoryHadNotBeenWrittenTo",
Message: "Blob not found",
}
return blob.AcceptOk{}, nil, NewAllocatedMemoryNotWrittenError()
}
log.Errorf("getting blob: %w", err)
return blob.AcceptOk{}, nil, failure.FromError(err)
Expand Down

0 comments on commit 6f51c2e

Please sign in to comment.