bytebase/bytebase · error

invalid prefix %q in request %q

Error message

invalid prefix %q in request %q

What it means

GetNameParentTokens parses a resource name (e.g. "projects/{project}/databases/{db}") into its parent token values. It validates that each odd-positioned segment is non-empty and that each even-positioned segment exactly matches the expected prefix (e.g. "projects/"). This error is thrown when a segment fails that prefix/emptiness check, meaning the resource name's shape does not match the expected pattern.

Source

Thrown at backend/common/resource_name.go:624

// TrimSuffix trims the suffix from the name and returns the trimmed name.
func TrimSuffix(name, suffix string) (string, error) {
	if !strings.HasSuffix(name, suffix) {
		return "", errors.Errorf("invalid request %q with suffix %q", name, suffix)
	}
	return strings.TrimSuffix(name, suffix), nil
}

// GetNameParentTokens returns the tokens from a resource name.
func GetNameParentTokens(name string, tokenPrefixes ...string) ([]string, error) {
	parts := strings.Split(name, "/")
	if len(parts) != 2*len(tokenPrefixes) {
		return nil, errors.Errorf("invalid request %q", name)
	}

	var tokens []string
	for i, tokenPrefix := range tokenPrefixes {
		if parts[2*i+1] == "" || fmt.Sprintf("%s/", parts[2*i]) != tokenPrefix {
			return nil, errors.Errorf("invalid prefix %q in request %q", tokenPrefix, name)
		}
		tokens = append(tokens, parts[2*i+1])
	}
	return tokens, nil
}

func GetWorkspaceID(name string) (string, error) {
	tokens, err := GetNameParentTokens(name, WorkspacePrefix)
	if err != nil {
		return "", err
	}
	return tokens[0], nil
}

func FormatWorkspace(id string) string {
	return fmt.Sprintf("%s%s", WorkspacePrefix, id)
}

View on GitHub (pinned to 1870550677)

Solutions

  1. Print the request name and compare each segment against the expected prefix (e.g. "projects/"); fix singular/plural or misspelled segments.
  2. Ensure every collection ID segment is non-empty (no double slashes).
  3. Use the library's own name-formatting helpers instead of building resource name strings by concatenation.
  4. Validate the name with a regexp like ^projects/[^/]+/databases/[^/]+$ before passing it to the API.

Example fix

// before
GetProjectID("project/abc")
// after
GetProjectID("projects/abc")
Defensive patterns

Strategy: validation

Validate before calling

var nameRe = regexp.MustCompile(`^projects/[^/]+(/databases/[^/]+)?$`)
func isValidResourceName(name string) bool { return nameRe.MatchString(name) && !strings.Contains(name, "//") }
// call GetProjectID only if isValidResourceName(name)

Type guard

func validTokens(name string) ([]string, bool) {
  parts := strings.Split(name, "/")
  if len(parts) < 2 || len(parts)%2 != 0 { return nil, false }
  for i := 0; i < len(parts); i += 2 {
    if parts[i] == "" || parts[i+1] == "" { return nil, false }
  }
  return parts, true
}

Try / catch

tokens, err := common.GetProjectID(name)
if err != nil {
  if strings.Contains(err.Error(), "invalid prefix") {
    // surface a 400 with the expected format, e.g. "projects/{project}"
  }
  return err
}

Prevention

When it happens

Trigger: Calling GetProjectID, GetEnvironmentID, GetProjectIDWebhookID, GetProjectIDDatabaseGroupID, GetProjectIDAccessGrantID, or GetProjectIDQueryHistoryID with a resource name whose collection segments don't literally match the expected prefixes (e.g. "project/abc" instead of "projects/abc") or contain empty identifiers ("projects//databases/x").

Common situations: Hand-constructed resource names in scripts or tests, singular-vs-plural typos ("project/" vs "projects/"), names copied from another API's format, or names missing an ID after a prefix.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06). Data as JSON: /api/errors/834a03a65c672e70. Report an issue: GitHub.