CodeSampleX

샘플

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

검증된 샘플 — golang google.golang.org/grpc v1.82.0: status.Code. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: status.Code returns codes.OK when…

sha256:a41d149540b57719a9895085e299839533d1ea904b121701f741035538cce954

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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.Code in pkg:golang/google.golang.org/grpc@v1.82.0
패키지
심벌
  • google.golang.org/grpc/status.Code
생성일
2026-10-12T07:53:21Z

컨트랙트

  1. status.Code returns codes.OK when passed a nil error.
  2. status.Code returns the matching codes.Code when passed a status error created with status.Error or status.New.
  3. status.Code returns codes.Unknown when passed a standard non-status error.
  4. status.Code unwraps errors wrapping a gRPC status error to return the inner status code.
  5. status.Code extracts the status code from custom error types implementing the GRPCStatus() *status.Status interface.

파일

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • 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.Code 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.Code

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:202e4bb781b6c9ba9a55beb1d45d4780da37b1a8e153cec7f46f1c4a60e4fedf","contract":["status.Code returns codes.OK when passed a nil error.","status.Code returns the matching codes.Code when passed a status error created with status.Error or status.New.","status.Code returns codes.Unknown when passed a standard non-status error.","status.Code unwraps errors wrapping a gRPC status error to return the inner status code.","status.Code extracts the status code from custom error types implementing the GRPCStatus() *status.Status interface."],"goal":"verify google.golang.org/grpc/status.Code 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.Code"]},"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.Code"],"verifierAdapter":"golang@1"}
go.mod
module example.com/statuscode

go 1.26.6

require google.golang.org/grpc v1.82.0

require (
	golang.org/x/sys v0.43.0 // indirect
	google.golang.org/genproto/googleapis/rpc v0.0.0-20260414002931-afd174a4e478 // 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.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.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-20260414002931-afd174a4e478 h1:RmoJA1ujG+/lRGNfUnOMfhCy5EipVMyvUE+KNbPbTlw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260414002931-afd174a4e478/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=
main.go
package main

import (
	"errors"
	"fmt"

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

type customError struct {
	code codes.Code
	msg  string
}

func (e *customError) Error() string {
	return e.msg
}

func (e *customError) GRPCStatus() *status.Status {
	return status.New(e.code, e.msg)
}

func main() {
	// 1. Nil error maps to codes.OK
	fmt.Printf("nil error code: %v\n", status.Code(nil))

	// 2. Status error maps to its specific code
	err := status.Error(codes.NotFound, "user not found")
	fmt.Printf("status.Error code: %v\n", status.Code(err))

	// 3. Generic error maps to codes.Unknown
	genericErr := errors.New("connection reset by peer")
	fmt.Printf("generic error code: %v\n", status.Code(genericErr))

	// 4. Wrapped status error unwraps to the underlying code
	wrappedErr := fmt.Errorf("service lookup failed: %w", err)
	fmt.Printf("wrapped error code: %v\n", status.Code(wrappedErr))

	// 5. Custom error with GRPCStatus method resolves to custom code
	custom := &customError{code: codes.PermissionDenied, msg: "insufficient permissions"}
	fmt.Printf("custom error code: %v\n", status.Code(custom))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/grpc/status.Code 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.Code"
  ]
}
test/contract.go
package main

import (
	"errors"
	"fmt"
	"os"

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

type customGRPCError struct {
	code codes.Code
	msg  string
}

func (e *customGRPCError) Error() string {
	return e.msg
}

func (e *customGRPCError) GRPCStatus() *status.Status {
	return status.New(e.code, e.msg)
}

func main() {
	// Assertion 1: status.Code returns codes.OK when passed a nil error.
	if code := status.Code(nil); code != codes.OK {
		fmt.Fprintf(os.Stderr, "FAIL assertion 1: expected codes.OK for nil error, got %v\n", code)
		os.Exit(1)
	}

	// Assertion 2: status.Code returns the matching codes.Code when passed a status error created with status.Error or status.New.
	canonicalCodes := []codes.Code{
		codes.Canceled,
		codes.Unknown,
		codes.InvalidArgument,
		codes.DeadlineExceeded,
		codes.NotFound,
		codes.AlreadyExists,
		codes.PermissionDenied,
		codes.ResourceExhausted,
		codes.FailedPrecondition,
		codes.Aborted,
		codes.OutOfRange,
		codes.Unimplemented,
		codes.Internal,
		codes.Unavailable,
		codes.DataLoss,
		codes.Unauthenticated,
	}

	for _, c := range canonicalCodes {
		err := status.Error(c, "status error message")
		if got := status.Code(err); got != c {
			fmt.Fprintf(os.Stderr, "FAIL assertion 2: status.Error(%v) returned code %v, want %v\n", c, got, c)
			os.Exit(1)
		}

		st := status.New(c, "status instance")
		if got := status.Code(st.Err()); got != c {
			fmt.Fprintf(os.Stderr, "FAIL assertion 2: status.New(%v).Err() returned code %v, want %v\n", c, got, c)
			os.Exit(1)
		}
	}

	// Assertion 3: status.Code returns codes.Unknown when passed a standard non-status error.
	genericErr := errors.New("arbitrary standard library error")
	if got := status.Code(genericErr); got != codes.Unknown {
		fmt.Fprintf(os.Stderr, "FAIL assertion 3: expected codes.Unknown for non-status error, got %v\n", got)
		os.Exit(1)
	}

	formattedErr := fmt.Errorf("formatted standard error: code %d", 42)
	if got := status.Code(formattedErr); got != codes.Unknown {
		fmt.Fprintf(os.Stderr, "FAIL assertion 3: expected codes.Unknown for formatted error, got %v\n", got)
		os.Exit(1)
	}

	// Assertion 4: status.Code unwraps errors wrapping a gRPC status error to return the inner status code.
	innerStatusErr := status.Error(codes.NotFound, "inner missing")
	wrappedErr := fmt.Errorf("context wrapper: %w", innerStatusErr)
	if got := status.Code(wrappedErr); got != codes.NotFound {
		fmt.Fprintf(os.Stderr, "FAIL assertion 4: expected codes.NotFound for wrapped error, got %v\n", got)
		os.Exit(1)
	}

	doubleWrappedErr := fmt.Errorf("outer layer: %w", fmt.Errorf("middle layer: %w", status.Error(codes.DeadlineExceeded, "timeout")))
	if got := status.Code(doubleWrappedErr); got != codes.DeadlineExceeded {
		fmt.Fprintf(os.Stderr, "FAIL assertion 4: expected codes.DeadlineExceeded for double wrapped error, got %v\n", got)
		os.Exit(1)
	}

	wrappedGeneric := fmt.Errorf("wrapped non-status: %w", errors.New("base failure"))
	if got := status.Code(wrappedGeneric); got != codes.Unknown {
		fmt.Fprintf(os.Stderr, "FAIL assertion 4: expected codes.Unknown for wrapped generic error, got %v\n", got)
		os.Exit(1)
	}

	// Assertion 5: status.Code extracts the status code from custom error types implementing the GRPCStatus() *status.Status interface.
	customErr := &customGRPCError{code: codes.PermissionDenied, msg: "access denied"}
	if got := status.Code(customErr); got != codes.PermissionDenied {
		fmt.Fprintf(os.Stderr, "FAIL assertion 5: expected codes.PermissionDenied for customGRPCError, got %v\n", got)
		os.Exit(1)
	}

	wrappedCustomErr := fmt.Errorf("outer handler: %w", customErr)
	if got := status.Code(wrappedCustomErr); got != codes.PermissionDenied {
		fmt.Fprintf(os.Stderr, "FAIL assertion 5: expected codes.PermissionDenied for wrapped custom error, got %v\n", got)
		os.Exit(1)
	}

	fmt.Println("All contract assertions verified successfully.")
}

오리진 시더

익명