Skip to content

Commit 0cd898f

Browse files
committed
chore: remome warning logs
Signed-off-by: Volodymyr Kit <[email protected]>
1 parent 76c309e commit 0cd898f

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

doc/did/doc.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import (
1212
"encoding/pem"
1313
"errors"
1414
"fmt"
15-
"log"
1615
"net/url"
17-
"os"
1816
"regexp"
1917
"strings"
2018
"time"
@@ -78,8 +76,6 @@ var (
7876
schemaLoaderV12019 = gojsonschema.NewStringLoader(schemaV12019) //nolint:gochecknoglobals
7977
)
8078

81-
var logger = log.New(os.Stderr, " [did-go/did/doc] ", log.Ldate|log.Ltime|log.LUTC)
82-
8379
// ErrDIDDocumentNotExist error did doc not exist.
8480
var ErrDIDDocumentNotExist = errors.New("did document not exists")
8581

@@ -290,6 +286,7 @@ type Doc struct {
290286
Updated *time.Time
291287
Proof []Proof
292288
processingMeta processingMeta
289+
warnings []error
293290
}
294291

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

@@ -1347,6 +1344,12 @@ func (doc *Doc) VerificationMethods(customVerificationRelationships ...Verificat
13471344
return verificationMethods
13481345
}
13491346

1347+
// Warning returns warnings in format of error suitable for use with methods such as errors.Is or errors.As
1348+
// returns nil if there were no warnings.
1349+
func (doc *Doc) Warning() error {
1350+
return errors.Join(doc.warnings...)
1351+
}
1352+
13501353
// ErrProofNotFound is returned when proof is not found.
13511354
var ErrProofNotFound = errors.New("proof not found")
13521355

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

13751378
// nolint:funlen,gocognit,gocyclo,nestif
1376-
func populateRawServices(services []Service, didID, baseURI string) []map[string]interface{} {
1379+
func (doc *Doc) populateRawServices() []map[string]interface{} {
1380+
var (
1381+
services = doc.Service
1382+
didID = doc.ID
1383+
baseURI = doc.processingMeta.baseURI
1384+
)
1385+
13771386
var rawServices []map[string]interface{}
13781387

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

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

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

14231434
if services[i].ServiceEndpoint.Type() == endpoint.DIDCommV2 {
@@ -1460,7 +1471,7 @@ func populateRawServices(services []Service, didID, baseURI string) []map[string
14601471
} else {
14611472
bytes, err := services[i].ServiceEndpoint.MarshalJSON()
14621473
if err != nil {
1463-
logger.Print(err.Error())
1474+
doc.warnings = append(doc.warnings, err)
14641475
}
14651476

14661477
rawService[jsonldServicePoint] = json.RawMessage(bytes)

doc/did/doc_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ func TestJSONConversion(t *testing.T) {
12681268
require.NotEmpty(t, doc2)
12691269

12701270
// verify documents created by ParseDocument and JSONBytes function matches
1271-
require.Equal(t, doc, doc2)
1271+
require.EqualExportedValues(t, doc, doc2)
12721272
}
12731273
}
12741274

@@ -1300,7 +1300,7 @@ func TestMarshalJSON(t *testing.T) {
13001300
require.NotEmpty(t, doc2)
13011301

13021302
// verify documents created by ParseDocument and JSONBytes function matches
1303-
require.Equal(t, doc, doc2)
1303+
require.EqualExportedValues(t, doc, doc2)
13041304
}
13051305
}
13061306

@@ -1313,6 +1313,30 @@ func TestUnmarshalJSON(t *testing.T) {
13131313
})
13141314
}
13151315

1316+
func TestDocWarning(t *testing.T) {
1317+
doc := &Doc{}
1318+
err := doc.UnmarshalJSON([]byte(validDoc))
1319+
require.NoError(t, err)
1320+
require.NotEmpty(t, doc)
1321+
1322+
_, err = doc.MarshalJSON()
1323+
require.NoError(t, err)
1324+
1325+
warning := doc.Warning()
1326+
1327+
for _, s := range doc.Service {
1328+
_, err := s.ServiceEndpoint.Accept()
1329+
if err != nil {
1330+
require.ErrorContains(t, warning, err.Error())
1331+
}
1332+
1333+
_, err = s.ServiceEndpoint.URI()
1334+
if err != nil {
1335+
require.ErrorContains(t, warning, err.Error())
1336+
}
1337+
}
1338+
}
1339+
13161340
func TestNewPublicKeyFromJWK(t *testing.T) {
13171341
pubKey, _, err := ed25519.GenerateKey(rand.Reader)
13181342
require.NoError(t, err)

0 commit comments

Comments
 (0)