goharbor/harbor · error

the resource cannot be null

Error message

the resource cannot be null

What it means

Returned by the Tencent TCR adapter's PrepareForPush (src/pkg/reg/adapter/tencentcr/adapter.go:215) when the resources slice contains a nil *model.Resource element. The method pre-creates the TCR namespace and repository before pushing, and dereferences each resource in the loop, so a nil entry is rejected first.

Source

Thrown at src/pkg/reg/adapter/tencentcr/adapter.go:215

			},
			{
				Type:  model.FilterTypeTag,
				Style: model.FilterStyleTypeText,
			},
		},
		SupportedTriggers: []string{
			model.TriggerTypeManual,
			model.TriggerTypeScheduled,
		},
	}
	return
}

func (a *adapter) PrepareForPush(resources []*model.Resource) (err error) {
	log.Debugf("[tencent-tcr.PrepareForPush]")
	for _, resource := range resources {
		if resource == nil {
			return errors.New("the resource cannot be null")
		}
		if resource.Metadata == nil {
			return errors.New("[tencent-tcr.PrepareForPush] the metadata of resource cannot be null")
		}
		if resource.Metadata.Repository == nil {
			return errors.New("[tencent-tcr.PrepareForPush] the namespace of resource cannot be null")
		}
		if len(resource.Metadata.Repository.Name) == 0 {
			return errors.New("[tencent-tcr.PrepareForPush] the name of the namespace cannot be null")
		}
		var paths = strings.Split(resource.Metadata.Repository.Name, "/")
		var namespace = paths[0]
		var repository = path.Join(paths[1:]...)

		log.Debugf("[tencent-tcr.PrepareForPush.createPrivateNamespace] namespace=%s", namespace)
		err = a.createPrivateNamespace(namespace)
		if err != nil {
			return

View on GitHub (pinned to 7b2fd08cc5)

Solutions

  1. Stop appending nil at the construction site; fail fast on fetch errors instead.
  2. Pre-validate the slice (see validationCode) to report the failing index.
  3. Keep one builder for resources shared by tests and production.
  4. Assert non-nil invariants in the pipeline's unit tests.

Example fix

// before
resources = append(resources, r) // r may be nil

// after
if r == nil {
    return fmt.Errorf("fetch returned nil resource")
}
resources = append(resources, r)
Defensive patterns

Strategy: validation

Validate before calling

for i, r := range resources {
	if r == nil {
		return fmt.Errorf("resources[%d] is nil", i)
	}
}

Type guard

func isUsableResource(r *model.Resource) bool { return r != nil }

Try / catch

if err := a.PrepareForPush(resources); err != nil {
	if strings.Contains(err.Error(), "the resource cannot be null") {
		// nil element: fix slice construction
	}
	return err
}

Prevention

When it happens

Trigger: Calling tencentcr PrepareForPush with a slice built by appending nil (e.g. conditional fetch results) instead of fully-formed resources.

Common situations: Custom transfer code appending placeholders on failure; sparse slices after filtering; refactors that introduce nil defaults; test tables with nil rows reaching shared code.

Related errors


AI-assisted analysis of goharbor/harbor@7b2fd08cc5 (2026-08-16). Data as JSON: /api/errors/8174c414dcefa285. Report an issue: GitHub.