Skip to main content

Result

The result package provides the standard API response envelope used throughout the framework.

Response Structure

Every API response follows this shape:

{
"code": 0,
"message": "Success",
"data": {}
}
type Result struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data"`
}

Creating Success Responses

import "github.com/coldsmirk/vef-framework-go/result"

// Simple success (no data)
return result.Ok().Response(ctx)

// Success with data
return result.Ok(user).Response(ctx)

// Success with custom message
return result.Ok(result.WithMessage("Created successfully")).Response(ctx)

// Success with data and custom message
return result.Ok(user, result.WithMessage("User created")).Response(ctx)

// Success with custom HTTP status
return result.Ok(user).Response(ctx, 201)

Ok Options

OptionEffect
result.WithMessage(msg)Override the default success message
result.WithCode(code)Override the default success code (0)

Creating Error Responses

// Simple business error
return result.Err("something went wrong").Response(ctx)

// Error with custom code
return result.Err("not found", result.WithErrCode(result.ErrCodeRecordNotFound)).Response(ctx)

// Error with custom HTTP status
return result.Err("unauthorized").Response(ctx, 401)

Err Options

OptionEffect
result.WithErrCode(code)Set a specific error code
result.WithErrData(data)Attach data to the error response

Checking Results

r := result.Ok(data)
r.IsOk() // true (code == 0)

r = result.Err("fail")
r.IsOk() // false

Error Codes

Authentication Errors (1000–1099)

CodeConstantMeaning
1000ErrCodeUnauthenticatedNot authenticated
1001ErrCodeUnsupportedAuthenticationTypeUnsupported auth type
1002ErrCodeTokenExpiredToken expired
1003ErrCodeTokenInvalidToken invalid
1004ErrCodeTokenNotValidYetToken not valid yet
1010ErrCodePrincipalInvalidPrincipal invalid
1011ErrCodeCredentialsInvalidCredentials invalid
1020ErrCodeSignatureInvalidSignature invalid

Challenge Errors (1030–1039)

CodeConstantMeaning
1030ErrCodeChallengeRequiredChallenge required
1035ErrCodeOTPCodeRequiredOTP code required
1036ErrCodeOTPCodeInvalidOTP code invalid
1037ErrCodeNewPasswordRequiredNew password required

Authorization Errors (1100–1199)

CodeConstantMeaning
1100ErrCodeAccessDeniedAccess denied

Request Errors (1200–1499)

CodeConstantMeaning
1200ErrCodeNotFoundResource not found
1300ErrCodeUnsupportedMediaTypeUnsupported media type
1400ErrCodeBadRequestBad request
1401ErrCodeTooManyRequestsRate limited
1402ErrCodeRequestTimeoutRequest timeout

Business Errors (2000+)

CodeConstantMeaning
2000ErrCodeDefaultGeneric business error
2001ErrCodeRecordNotFoundRecord not found in database
2002ErrCodeRecordAlreadyExistsDuplicate record
2003ErrCodeForeignKeyViolationForeign key constraint violation

I18n Integration

Error and success messages are automatically localized through the i18n module. The message keys (e.g., "record_not_found", "success") are looked up in the configured language bundle at runtime.

Pre-built Error Constructors

The framework provides common error constructors:

result.ErrRecordNotFound()       // code: 2001
result.ErrRecordAlreadyExists() // code: 2002
result.ErrForeignKeyViolation() // code: 2003
result.ErrAccessDenied() // code: 1100
result.ErrUnauthenticated() // code: 1000
result.ErrBadRequest(msg) // code: 1400
result.ErrNotFound() // code: 1200
result.ErrTooManyRequests() // code: 1401