CodeSampleX

サンプル

google.golang.org/grpc v1.82.0: status.Status

検証済みサンプル — golang google.golang.org/grpc v1.82.0: status.Status. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました: status.New constructs a Status…

sha256:e07d68e6d59f27b002df3668abc7c6a0d0ff13a90194604d5331cedb54142832

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 go

検証実行環境

環境 コントラクト ステージ 実行日
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-10-12

ケース

HOW
ゴール
verify google.golang.org/grpc/status.Status in pkg:golang/google.golang.org/grpc@v1.82.0
パッケージ
シンボル
  • google.golang.org/grpc/status.Status
作成日
2026-10-12T13:35:31Z

コントラクト

  1. status.New constructs a Status object with the given code and message
  2. status.Newf constructs a formatted Status object with formatting arguments
  3. Status.Err returns an error representing the Status and status.FromError retrieves the original Status
  4. status.Code extracts the status code from error or returns OK for nil error
  5. status.Convert converts errors into Status instances returning nil for nil error

ファイル

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • status.go
  • test/contract.go

ソースアーティファクトをダウンロード (tar.gz)

ソース

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify google.golang.org/grpc/status.Status in pkg:golang/google.golang.org/grpc@v1.82.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/grpc@v1.82.0
Demonstrate these symbols/APIs:
  - google.golang.org/grpc/status.Status

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:4b464a8cde2a90f02f979f0ad1272ee849381b72edb20b1a128c96159aba1230","contract":["status.New constructs a Status object with the given code and message","status.Newf constructs a formatted Status object with formatting arguments","Status.Err returns an error representing the Status and status.FromError retrieves the original Status","status.Code extracts the status code from error or returns OK for nil error","status.Convert converts errors into Status instances returning nil for nil error"],"goal":"verify google.golang.org/grpc/status.Status in pkg:golang/google.golang.org/grpc@v1.82.0","kind":"HOW","packages":["pkg:golang/google.golang.org/grpc@v1.82.0"],"schemaVersion":1,"symbols":["google.golang.org/grpc/status.Status"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/google.golang.org/grpc@v1.82.0"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/grpc@v1.82.0","symbols":["google.golang.org/grpc/status.Status"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require google.golang.org/grpc v1.82.0

require (
	golang.org/x/sys v0.47.0 // indirect
	google.golang.org/genproto/googleapis/rpc v0.0.0-20260706201446-f0a921348800 // indirect
	google.golang.org/protobuf v1.36.11 // indirect
)
go.sum
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260706201446-f0a921348800 h1:qEHAMpSaUhtD0p3NbEEI83HwNGFxEwaSJ1G9PLnCBZE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260706201446-f0a921348800/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
google.golang.org/grpc v1.82.0 h1:vguDnZUPjE26w09A63VoxZPnvPjB5Riyc0mkXPFmAIU=
google.golang.org/grpc v1.82.0/go.mod h1:yzTZ1TB1Z3SG+LIYaI+WiE8D5+PZ3ArnrSp8zF3+/ZA=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/grpc/status.Status in pkg:golang/google.golang.org/grpc@v1.82.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/grpc@v1.82.0"
  ],
  "symbols": [
    "google.golang.org/grpc/status.Status"
  ]
}
status.go
package sample

import (
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// NewStatus creates a new Status pointer with the given code and message.
func NewStatus(code codes.Code, msg string) *status.Status {
	return status.New(code, msg)
}

// NewStatusf creates a formatted Status pointer with the given code and format string.
func NewStatusf(code codes.Code, format string, args ...any) *status.Status {
	return status.Newf(code, format, args...)
}

// StatusToError converts a Status to an error interface.
func StatusToError(s *status.Status) error {
	if s == nil {
		return nil
	}
	return s.Err()
}

// ErrorToStatus extracts a Status from an error if possible.
func ErrorToStatus(err error) (*status.Status, bool) {
	return status.FromError(err)
}

// ConvertError converts any error into a Status pointer.
func ConvertError(err error) *status.Status {
	return status.Convert(err)
}

// GetCode extracts the status code from an error.
func GetCode(err error) codes.Code {
	return status.Code(err)
}
test/contract.go
package main

import (
	"errors"
	"fmt"
	"os"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"sample"
)

func main() {
	fmt.Println("Running contract tests for google.golang.org/grpc/status.Status...")

	// Assertion 1: status.New constructs a Status object with the given code and message
	{
		var st *status.Status = sample.NewStatus(codes.NotFound, "entity not found")
		if st == nil {
			fmt.Fprintf(os.Stderr, "Assertion 1 failed: status is nil\n")
			os.Exit(1)
		}
		if st.Code() != codes.NotFound {
			fmt.Fprintf(os.Stderr, "Assertion 1 failed: expected code NotFound, got %v\n", st.Code())
			os.Exit(1)
		}
		if st.Message() != "entity not found" {
			fmt.Fprintf(os.Stderr, "Assertion 1 failed: expected message 'entity not found', got '%s'\n", st.Message())
			os.Exit(1)
		}
		if st.Proto().GetCode() != int32(codes.NotFound) {
			fmt.Fprintf(os.Stderr, "Assertion 1 failed: proto code mismatch: %d\n", st.Proto().GetCode())
			os.Exit(1)
		}
		if st.Proto().GetMessage() != "entity not found" {
			fmt.Fprintf(os.Stderr, "Assertion 1 failed: proto message mismatch: '%s'\n", st.Proto().GetMessage())
			os.Exit(1)
		}
		fmt.Println("  ✓ Assertion 1 passed: status.New constructs Status with code and message")
	}

	// Assertion 2: status.Newf constructs a formatted Status object with formatting arguments
	{
		st := sample.NewStatusf(codes.InvalidArgument, "invalid item id: %d", 42)
		if st == nil {
			fmt.Fprintf(os.Stderr, "Assertion 2 failed: status is nil\n")
			os.Exit(1)
		}
		if st.Code() != codes.InvalidArgument {
			fmt.Fprintf(os.Stderr, "Assertion 2 failed: expected code InvalidArgument, got %v\n", st.Code())
			os.Exit(1)
		}
		if st.Message() != "invalid item id: 42" {
			fmt.Fprintf(os.Stderr, "Assertion 2 failed: expected message 'invalid item id: 42', got '%s'\n", st.Message())
			os.Exit(1)
		}
		fmt.Println("  ✓ Assertion 2 passed: status.Newf constructs formatted Status")
	}

	// Assertion 3: Status.Err returns an error representing the Status and status.FromError retrieves the original Status
	{
		st := sample.NewStatus(codes.PermissionDenied, "access denied")
		err := sample.StatusToError(st)
		if err == nil {
			fmt.Fprintf(os.Stderr, "Assertion 3 failed: expected non-nil error from non-OK status\n")
			os.Exit(1)
		}
		expectedErrStr := "rpc error: code = PermissionDenied desc = access denied"
		if err.Error() != expectedErrStr {
			fmt.Fprintf(os.Stderr, "Assertion 3 failed: expected '%s', got '%s'\n", expectedErrStr, err.Error())
			os.Exit(1)
		}

		extracted, ok := sample.ErrorToStatus(err)
		if !ok || extracted == nil {
			fmt.Fprintf(os.Stderr, "Assertion 3 failed: status.FromError failed to extract status\n")
			os.Exit(1)
		}
		if extracted.Code() != codes.PermissionDenied {
			fmt.Fprintf(os.Stderr, "Assertion 3 failed: extracted code mismatch: %v\n", extracted.Code())
			os.Exit(1)
		}
		if extracted.Message() != "access denied" {
			fmt.Fprintf(os.Stderr, "Assertion 3 failed: extracted message mismatch: '%s'\n", extracted.Message())
			os.Exit(1)
		}

		// Also verify that OK status returns nil error
		okStatus := sample.NewStatus(codes.OK, "")
		if okErr := sample.StatusToError(okStatus); okErr != nil {
			fmt.Fprintf(os.Stderr, "Assertion 3 failed: OK status should produce nil error, got %v\n", okErr)
			os.Exit(1)
		}
		fmt.Println("  ✓ Assertion 3 passed: Status.Err and status.FromError bidirectional conversion")
	}

	// Assertion 4: status.Code extracts the status code from error or returns OK for nil error
	{
		if c := sample.GetCode(nil); c != codes.OK {
			fmt.Fprintf(os.Stderr, "Assertion 4 failed: nil error should return OK, got %v\n", c)
			os.Exit(1)
		}

		st := sample.NewStatus(codes.ResourceExhausted, "quota exceeded")
		if c := sample.GetCode(sample.StatusToError(st)); c != codes.ResourceExhausted {
			fmt.Fprintf(os.Stderr, "Assertion 4 failed: expected ResourceExhausted, got %v\n", c)
			os.Exit(1)
		}

		plainErr := errors.New("arbitrary standard error")
		if c := sample.GetCode(plainErr); c != codes.Unknown {
			fmt.Fprintf(os.Stderr, "Assertion 4 failed: plain error should return Unknown, got %v\n", c)
			os.Exit(1)
		}
		fmt.Println("  ✓ Assertion 4 passed: status.Code extracts code from errors")
	}

	// Assertion 5: status.Convert converts errors into Status instances returning nil for nil error
	{
		convNil := sample.ConvertError(nil)
		if convNil != nil {
			fmt.Fprintf(os.Stderr, "Assertion 5 failed: Convert(nil) should yield nil, got %v\n", convNil)
			os.Exit(1)
		}

		st := sample.NewStatus(codes.Unauthenticated, "missing bearer token")
		convGrpc := sample.ConvertError(sample.StatusToError(st))
		if convGrpc == nil || convGrpc.Code() != codes.Unauthenticated || convGrpc.Message() != "missing bearer token" {
			fmt.Fprintf(os.Stderr, "Assertion 5 failed: Convert(grpcErr) mismatch\n")
			os.Exit(1)
		}

		plainErr := errors.New("database timeout")
		convPlain := sample.ConvertError(plainErr)
		if convPlain == nil || convPlain.Code() != codes.Unknown || convPlain.Message() != "database timeout" {
			fmt.Fprintf(os.Stderr, "Assertion 5 failed: Convert(plainErr) mismatch\n")
			os.Exit(1)
		}
		fmt.Println("  ✓ Assertion 5 passed: status.Convert converts errors returning nil for nil error")
	}

	fmt.Println("All contract tests passed successfully!")
}

オリジンシーダー

匿名