Skip to content

Commit

Permalink
chore: remome warning logs
Browse files Browse the repository at this point in the history
Signed-off-by: Volodymyr Kit <[email protected]>
  • Loading branch information
justakit committed Jan 10, 2025
1 parent 76c309e commit 0cd898f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
29 changes: 20 additions & 9 deletions doc/did/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
"encoding/pem"
"errors"
"fmt"
"log"
"net/url"
"os"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -78,8 +76,6 @@ var (
schemaLoaderV12019 = gojsonschema.NewStringLoader(schemaV12019) //nolint:gochecknoglobals
)

var logger = log.New(os.Stderr, " [did-go/did/doc] ", log.Ldate|log.Ltime|log.LUTC)

// ErrDIDDocumentNotExist error did doc not exist.
var ErrDIDDocumentNotExist = errors.New("did document not exists")

Expand Down Expand Up @@ -290,6 +286,7 @@ type Doc struct {
Updated *time.Time
Proof []Proof
processingMeta processingMeta
warnings []error
}

// processingMeta include info how to process the doc.
Expand Down Expand Up @@ -1221,7 +1218,7 @@ func (doc *Doc) JSONBytes() ([]byte, error) {
Context: doc.Context, ID: doc.ID, AlsoKnownAs: aka, VerificationMethod: vm,
Authentication: auths, AssertionMethod: assertionMethods, CapabilityDelegation: capabilityDelegations,
CapabilityInvocation: capabilityInvocations, KeyAgreement: keyAgreements,
Service: populateRawServices(doc.Service, doc.ID, doc.processingMeta.baseURI), Created: doc.Created,
Service: doc.populateRawServices(), Created: doc.Created,
Proof: populateRawProofs(context, doc.ID, doc.processingMeta.baseURI, doc.Proof), Updated: doc.Updated,
}

Expand Down Expand Up @@ -1347,6 +1344,12 @@ func (doc *Doc) VerificationMethods(customVerificationRelationships ...Verificat
return verificationMethods
}

// Warning returns warnings in format of error suitable for use with methods such as errors.Is or errors.As
// returns nil if there were no warnings.
func (doc *Doc) Warning() error {
return errors.Join(doc.warnings...)
}

// ErrProofNotFound is returned when proof is not found.
var ErrProofNotFound = errors.New("proof not found")

Expand All @@ -1373,7 +1376,13 @@ func (r *didKeyResolver) Resolve(id string) (*api.PublicKey, error) {
var ErrKeyNotFound = errors.New("key not found")

// nolint:funlen,gocognit,gocyclo,nestif
func populateRawServices(services []Service, didID, baseURI string) []map[string]interface{} {
func (doc *Doc) populateRawServices() []map[string]interface{} {
var (
services = doc.Service
didID = doc.ID
baseURI = doc.processingMeta.baseURI
)

var rawServices []map[string]interface{}

for i := range services {
Expand Down Expand Up @@ -1412,12 +1421,14 @@ func populateRawServices(services []Service, didID, baseURI string) []map[string

sepAccept, err := services[i].ServiceEndpoint.Accept()
if err != nil {
logger.Printf("accept field of DIDComm V2 endpoint missing or invalid, it will be ignored: %v", err)
doc.warnings = append(doc.warnings,
fmt.Errorf("accept field of DIDComm V2 endpoint missing or invalid, it will be ignored: %w", err))
}

sepURI, err := services[i].ServiceEndpoint.URI()
if err != nil {
logger.Printf("URI field of DIDComm V2 endpoint missing or invalid, it will be ignored: %v", err)
doc.warnings = append(doc.warnings,
fmt.Errorf("URI field of DIDComm V2 endpoint missing or invalid, it will be ignored: %w", err))
}

if services[i].ServiceEndpoint.Type() == endpoint.DIDCommV2 {
Expand Down Expand Up @@ -1460,7 +1471,7 @@ func populateRawServices(services []Service, didID, baseURI string) []map[string
} else {
bytes, err := services[i].ServiceEndpoint.MarshalJSON()
if err != nil {
logger.Print(err.Error())
doc.warnings = append(doc.warnings, err)
}

rawService[jsonldServicePoint] = json.RawMessage(bytes)
Expand Down
28 changes: 26 additions & 2 deletions doc/did/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ func TestJSONConversion(t *testing.T) {
require.NotEmpty(t, doc2)

// verify documents created by ParseDocument and JSONBytes function matches
require.Equal(t, doc, doc2)
require.EqualExportedValues(t, doc, doc2)
}
}

Expand Down Expand Up @@ -1300,7 +1300,7 @@ func TestMarshalJSON(t *testing.T) {
require.NotEmpty(t, doc2)

// verify documents created by ParseDocument and JSONBytes function matches
require.Equal(t, doc, doc2)
require.EqualExportedValues(t, doc, doc2)
}
}

Expand All @@ -1313,6 +1313,30 @@ func TestUnmarshalJSON(t *testing.T) {
})
}

func TestDocWarning(t *testing.T) {
doc := &Doc{}
err := doc.UnmarshalJSON([]byte(validDoc))
require.NoError(t, err)
require.NotEmpty(t, doc)

_, err = doc.MarshalJSON()
require.NoError(t, err)

warning := doc.Warning()

for _, s := range doc.Service {
_, err := s.ServiceEndpoint.Accept()
if err != nil {
require.ErrorContains(t, warning, err.Error())
}

_, err = s.ServiceEndpoint.URI()
if err != nil {
require.ErrorContains(t, warning, err.Error())
}
}
}

func TestNewPublicKeyFromJWK(t *testing.T) {
pubKey, _, err := ed25519.GenerateKey(rand.Reader)
require.NoError(t, err)
Expand Down

0 comments on commit 0cd898f

Please sign in to comment.