Skip to content

panic while iterating over Page Item #558

@vijayrajah

Description

@vijayrajah

i want to get a list of apps.

Here is my code

func getAPPs(ctx context.Context) error {

	opts := applications.ApplicationsRequestBuilderGetRequestConfiguration{
		QueryParameters: &applications.ApplicationsRequestBuilderGetQueryParameters{
			Select: []string{"displayName", "appId", "id", "keyCredentials", "passwordCredentials"},
			Top:    to.Ptr[int32](100),
		},
	}

	resp, err := client.Applications().Get(ctx, &opts)
	if err != nil {
		printOdataError(err)
		return errors.Join(err, fmt.Errorf("unable to get list of Apps"))
	}

	pIter, err := msgraphgocore.NewPageIterator[*models.Application](resp, client.GetAdapter(), models.CreateApplicationFromDiscriminatorValue)
	if err != nil {
		return errors.Join(err, fmt.Errorf("unable to create iterator for app"))
	}

	cnt := 1
	err = pIter.Iterate(ctx,
		func(pageItem *models.Application) bool {
			fmt.Printf("ID: %s, name: %s, cnt: %d\n", *pageItem.GetAppId(), *pageItem.GetDisplayName(), cnt)
			cnt++
			return true
		},
	)

	
	return nil

}

This prints all the 100 App details & Panics.

panic: reflect: call of reflect.Value.IsNil on zero Value

goroutine 1 [running]:
reflect.Value.IsNil({0x0, 0x0, 0x0})
        C:/Users/<username>/go_1.21.0/src/reflect/value.go:1568 +0x172
github.com/microsoftgraph/msgraph-sdk-go-core.convertToPage[...]({0x288ad60, 0xc001064130})
        C:/Users/<username>/go/pkg/mod/github.com/microsoftgraph/msgraph-sdk-go-core@v1.0.0/page_iterator.go:219 +0xe5
github.com/microsoftgraph/msgraph-sdk-go-core.(*PageIterator[...]).next(0x29befe0, {0x29baa48, 0xc0001f07d0})
        C:/Users/<username>/go/pkg/mod/github.com/microsoftgraph/msgraph-sdk-go-core@v1.0.0/page_iterator.go:136 +0x245
github.com/microsoftgraph/msgraph-sdk-go-core.(*PageIterator[...]).Iterate(0x29befe0, {0x29baa48, 0xc0001f07d0}, 0xc00034bc28)
        C:/Users/<username>/go/pkg/mod/github.com/microsoftgraph/msgraph-sdk-go-core@v1.0.0/page_iterator.go:96 +0x174
main.getAPPs({0x29baa48, 0xc0001f07d0})
        C:/Users/<username>/GolandProjects/az-ad-principal-service/ad.go:43 +0x753
main.main()
        C:/Users/<username>/GolandProjects/az-ad-principal-service/main.go:127 +0x625

it seems I need to stop the execution of the loop.

How do I do that? is there an example on how to do that ? (if I do not know how many Apps I have, like when getting all the apps in my org)

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions