Skip to content

Commit

Permalink
decrease error handling complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Aug 20, 2023
1 parent c48b1bd commit c4fcda1
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 58 deletions.
6 changes: 3 additions & 3 deletions datamodel/high/v3/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func TestStripeAsDoc(t *testing.T) {
info, _ := datamodel.ExtractSpecInfo(data)
var err error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
assert.Len(t, errorutils.Unwrap(err), 3)
assert.Len(t, errorutils.ShallowUnwrap(err), 3)
d := NewDocument(lowDoc)
assert.NotNil(t, d)
}
Expand All @@ -397,7 +397,7 @@ func TestK8sAsDoc(t *testing.T) {
var err error
lowSwag, err := lowv2.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
d := v2.NewSwaggerDocument(lowSwag)
assert.Len(t, errorutils.Unwrap(err), 0)
assert.Len(t, errorutils.ShallowUnwrap(err), 0)
assert.NotNil(t, d)
}

Expand Down Expand Up @@ -452,7 +452,7 @@ func TestCircularReferencesDoc(t *testing.T) {
info, _ := datamodel.ExtractSpecInfo(data)
var err error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
assert.Len(t, errorutils.Unwrap(err), 3)
assert.Len(t, errorutils.ShallowUnwrap(err), 3)
d := NewDocument(lowDoc)
assert.Len(t, d.Components.Schemas, 9)
assert.Len(t, d.Index.GetCircularReferences(), 3)
Expand Down
20 changes: 10 additions & 10 deletions datamodel/low/v2/swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestCreateDocument_ExternalDocsBad(t *testing.T) {
wait = false
}
}
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_TagsBad(t *testing.T) {
Expand All @@ -211,7 +211,7 @@ func TestCreateDocument_TagsBad(t *testing.T) {
wait = false
}
}
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_PathsBad(t *testing.T) {
Expand All @@ -233,7 +233,7 @@ func TestCreateDocument_PathsBad(t *testing.T) {
wait = false
}
}
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_SecurityBad(t *testing.T) {
Expand All @@ -251,7 +251,7 @@ func TestCreateDocument_SecurityBad(t *testing.T) {
wait = false
}
}
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_SecurityDefinitionsBad(t *testing.T) {
Expand All @@ -269,7 +269,7 @@ func TestCreateDocument_SecurityDefinitionsBad(t *testing.T) {
wait = false
}
}
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_ResponsesBad(t *testing.T) {
Expand All @@ -287,7 +287,7 @@ func TestCreateDocument_ResponsesBad(t *testing.T) {
wait = false
}
}
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_ParametersBad(t *testing.T) {
Expand All @@ -305,7 +305,7 @@ func TestCreateDocument_ParametersBad(t *testing.T) {
wait = false
}
}
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_DefinitionsBad(t *testing.T) {
Expand All @@ -323,7 +323,7 @@ func TestCreateDocument_DefinitionsBad(t *testing.T) {
wait = false
}
}
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_InfoBad(t *testing.T) {
Expand All @@ -341,7 +341,7 @@ func TestCreateDocument_InfoBad(t *testing.T) {
wait = false
}
}
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCircularReferenceError(t *testing.T) {
Expand All @@ -350,6 +350,6 @@ func TestCircularReferenceError(t *testing.T) {
info, _ := datamodel.ExtractSpecInfo(data)
circDoc, err := CreateDocument(info)
assert.NotNil(t, circDoc)
assert.Len(t, errorutils.Unwrap(err), 3)
assert.Len(t, errorutils.ShallowUnwrap(err), 3)

}
22 changes: 11 additions & 11 deletions datamodel/low/v3/create_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCircularReferenceError(t *testing.T) {
AllowFileReferences: false,
AllowRemoteReferences: false,
})
assert.Len(t, errorutils.Unwrap(err), 3)
assert.Len(t, errorutils.ShallowUnwrap(err), 3)

// lower level packages return errors and potentially a document.
assert.NotNil(t, circDoc)
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestCreateDocumentStripe(t *testing.T) {
AllowRemoteReferences: false,
BasePath: "/here",
})
assert.Len(t, errorutils.Unwrap(err), 3)
assert.Len(t, errorutils.ShallowUnwrap(err), 3)

assert.Equal(t, "3.0.0", d.Version.Value)
assert.Equal(t, "Stripe API", d.Info.Value.Title.Value)
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestCreateDocument_WebHooks_Error(t *testing.T) {
AllowFileReferences: false,
AllowRemoteReferences: false,
})
require.Len(t, errorutils.Unwrap(err), 1)
require.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_Servers(t *testing.T) {
Expand Down Expand Up @@ -572,7 +572,7 @@ components:
AllowRemoteReferences: false,
})
require.NoError(t, err)
require.Len(t, errorutils.Unwrap(err), 0)
require.Len(t, errorutils.ShallowUnwrap(err), 0)

ob := doc.Components.Value.FindSchema("bork").Value
ob.Schema()
Expand All @@ -591,7 +591,7 @@ webhooks:
AllowFileReferences: false,
AllowRemoteReferences: false,
})
require.Len(t, errorutils.Unwrap(err), 1)
require.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_Components_Error_Extract(t *testing.T) {
Expand All @@ -607,7 +607,7 @@ components:
AllowFileReferences: false,
AllowRemoteReferences: false,
})
require.Len(t, errorutils.Unwrap(err), 1)
require.Len(t, errorutils.ShallowUnwrap(err), 1)

}

Expand All @@ -623,7 +623,7 @@ paths:
AllowFileReferences: false,
AllowRemoteReferences: false,
})
require.Len(t, errorutils.Unwrap(err), 1)
require.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_Tags_Errors(t *testing.T) {
Expand All @@ -637,7 +637,7 @@ tags:
AllowFileReferences: false,
AllowRemoteReferences: false,
})
require.Len(t, errorutils.Unwrap(err), 1)
require.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_Security_Error(t *testing.T) {
Expand All @@ -651,7 +651,7 @@ security:
AllowFileReferences: false,
AllowRemoteReferences: false,
})
require.Len(t, errorutils.Unwrap(err), 1)
require.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_ExternalDoc_Error(t *testing.T) {
Expand All @@ -665,7 +665,7 @@ externalDocs:
AllowFileReferences: false,
AllowRemoteReferences: false,
})
require.Len(t, errorutils.Unwrap(err), 1)
require.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestCreateDocument_YamlAnchor(t *testing.T) {
Expand Down Expand Up @@ -746,7 +746,7 @@ func ExampleCreateDocument() {
}

func panicOnUnknown(err error) {
for _, err := range errorutils.Unwrap(err) {
for _, err := range errorutils.ShallowUnwrap(err) {
var resolvErr *resolver.ResolvingError
if errors.As(err, &resolvErr) && resolvErr.CircularReference != nil {
continue
Expand Down
30 changes: 15 additions & 15 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestLoadDocument_Simple_V2(t *testing.T) {

v2Doc, docErr := doc.BuildV2Model()
require.Nil(t, docErr)
require.Len(t, errorutils.Unwrap(docErr), 0)
require.Len(t, errorutils.ShallowUnwrap(docErr), 0)
require.NotNil(t, v2Doc)
require.NotNil(t, doc.GetSpecInfo())
}
Expand All @@ -36,7 +36,7 @@ func TestLoadDocument_Simple_V2_Error(t *testing.T) {
assert.NoError(t, err)

v2Doc, docErr := doc.BuildV3Model()
assert.Len(t, errorutils.Unwrap(docErr), 1)
assert.Len(t, errorutils.ShallowUnwrap(docErr), 1)
assert.Nil(t, v2Doc)
}

Expand All @@ -50,7 +50,7 @@ definitions:
require.NoError(t, err)

v2Doc, docErr := doc.BuildV2Model()
require.Len(t, errorutils.Unwrap(docErr), 2)
require.Len(t, errorutils.ShallowUnwrap(docErr), 2)
require.Nil(t, v2Doc)
}

Expand All @@ -61,22 +61,22 @@ func TestLoadDocument_Simple_V3_Error(t *testing.T) {
assert.NoError(t, err)

v2Doc, docErr := doc.BuildV2Model()
assert.Len(t, errorutils.Unwrap(docErr), 1)
assert.Len(t, errorutils.ShallowUnwrap(docErr), 1)
assert.Nil(t, v2Doc)
}

func TestLoadDocument_Error_V2NoSpec(t *testing.T) {

doc := new(document) // not how this should be instantiated.
_, err := doc.BuildV2Model()
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestLoadDocument_Error_V3NoSpec(t *testing.T) {

doc := new(document) // not how this should be instantiated.
_, err := doc.BuildV3Model()
assert.Len(t, errorutils.Unwrap(err), 1)
assert.Len(t, errorutils.ShallowUnwrap(err), 1)
}

func TestLoadDocument_Empty(t *testing.T) {
Expand All @@ -93,7 +93,7 @@ func TestLoadDocument_Simple_V3(t *testing.T) {
assert.Equal(t, "3.0.1", doc.GetVersion())

v3Doc, docErr := doc.BuildV3Model()
assert.Len(t, errorutils.Unwrap(docErr), 0)
assert.Len(t, errorutils.ShallowUnwrap(docErr), 0)
assert.NotNil(t, v3Doc)
}

Expand All @@ -107,7 +107,7 @@ paths:
assert.NoError(t, err)

v3Doc, docErr := doc.BuildV3Model()
assert.Len(t, errorutils.Unwrap(docErr), 2)
assert.Len(t, errorutils.ShallowUnwrap(docErr), 2)
assert.Nil(t, v3Doc)
}

Expand Down Expand Up @@ -302,7 +302,7 @@ func TestDocument_BuildModelPreBuild(t *testing.T) {
require.NoError(t, err)
_, _, _, err = doc.RenderAndReload()
assert.Nil(t, err)
assert.Len(t, errorutils.Unwrap(err), 0)
assert.Len(t, errorutils.ShallowUnwrap(err), 0)
}

func TestDocument_AnyDoc(t *testing.T) {
Expand All @@ -328,7 +328,7 @@ func TestDocument_BuildModelCircular(t *testing.T) {
// top level library does not return broken objects
// with an error, only one or the other
assert.Nil(t, m)
assert.Len(t, errorutils.Unwrap(err), 3)
assert.Len(t, errorutils.ShallowUnwrap(err), 3)
}

func TestDocument_BuildModelBad(t *testing.T) {
Expand All @@ -337,7 +337,7 @@ func TestDocument_BuildModelBad(t *testing.T) {
require.NoError(t, err)
m, err := doc.BuildV3Model()
assert.Nil(t, m)
assert.Len(t, errorutils.Unwrap(err), 9)
assert.Len(t, errorutils.ShallowUnwrap(err), 9)
}

func TestDocument_Serialize_JSON_Modified(t *testing.T) {
Expand Down Expand Up @@ -407,7 +407,7 @@ func TestDocument_BuildModel_CompareDocsV3_LeftError(t *testing.T) {
WithAllowFileReferences(true),
)
changes, err := CompareDocuments(originalDoc, updatedDoc)
require.Len(t, errorutils.Unwrap(err), 9)
require.Len(t, errorutils.ShallowUnwrap(err), 9)
require.Nil(t, changes)
}

Expand All @@ -418,7 +418,7 @@ func TestDocument_BuildModel_CompareDocsV3_RightError(t *testing.T) {
originalDoc, _ := NewDocument(burgerShopOriginal)
updatedDoc, _ := NewDocument(burgerShopUpdated)
changes, err := CompareDocuments(updatedDoc, originalDoc)
require.Len(t, errorutils.Unwrap(err), 9)
require.Len(t, errorutils.ShallowUnwrap(err), 9)
require.Nil(t, changes)
}

Expand All @@ -429,7 +429,7 @@ func TestDocument_BuildModel_CompareDocsV2_Error(t *testing.T) {
originalDoc, _ := NewDocument(burgerShopOriginal)
updatedDoc, _ := NewDocument(burgerShopUpdated)
changes, err := CompareDocuments(updatedDoc, originalDoc)
require.Len(t, errorutils.Unwrap(err), 2)
require.Len(t, errorutils.ShallowUnwrap(err), 2)
require.Nil(t, changes)

}
Expand All @@ -446,7 +446,7 @@ func TestDocument_BuildModel_CompareDocsV2V3Mix_Error(t *testing.T) {
require.NoError(t, err)

changes, err := CompareDocuments(updatedDoc, originalDoc)
require.Len(t, errorutils.Unwrap(err), 1)
require.Len(t, errorutils.ShallowUnwrap(err), 1)
require.Nil(t, changes)
}

Expand Down
Loading

0 comments on commit c4fcda1

Please sign in to comment.