开发者文档
ERROR CODES
TronEnergy API错误代码完整列表,包含描述和解决方法。
每个错误响应都包含两个字段: error (机器可读、稳定、版本间永不改变) message (便于阅读,可能会随着时间推移而改进)。始终开启 error 在你的代码中显示 message 对用户而言。
错误格式
所有错误响应都遵循相同的结构:
error response
{
"error": "error_code_here",
"message": "Human-readable explanation"
}
部分错误信息包含额外的字段: ref (委托尝试的参考 ID) refund (有关自动退款的详细信息)
验证错误
| 错误代码 | HTTP | 原因 | 解决 |
|---|---|---|---|
invalid_tx_hash | 400 | tx_hash 不是一个 64 个字符的十六进制字符串。 | 检查哈希格式。必须正好是 64 个十六进制字符,不能有前缀。 |
invalid_address | 400 | delegate_to 不是有效的Tron地址 | 调用前请使用TronWeb () 验证地址。 |
missing_signature | 400 | 申请中未提供签名。 | 签署此信息 {tx_hash}:{delegate_to} 和 tronWeb.trx.signMessageV2() 从发送TRX的钱包中。 |
invalid_signature | 401 | 签名无法验证 | 请务必准确签名。 {tx_hash}:{delegate_to} (小写十六进制哈希值,冒号,准确的Tron地址)。 |
signature_mismatch | 403 | 签名人地址与付款人地址不符 | 签名必须来自发送TRX付款的同一钱包。来自不同钱包的签名将被拒绝。 |
支付错误
| 错误代码 | HTTP | 原因 | 解决 |
|---|---|---|---|
payment_verification_failed | 404 / 400 | 链上支付无法验证。 message 该字段用于描述具体原因。 | 常见原因:交易尚未确认(请等待 3-5 秒后重试一次),收款人地址错误,交易不是TRX转账,低于 4 TRX最低限额。 |
hash_already_used | 409 | 此交易哈希已被认领 | 每个支付哈希值只能使用一次。请为新的委托发送新的支付。 |
服务错误
| 错误代码 | HTTP | 原因 | 解决 |
|---|---|---|---|
delegation_failed | 400 / 500 | 供应商无法交付能源委托 | 如果付款验证后出现故障,系统会自动将退款加入队列。请查看 refund 否则请重试,或联系支持人员。 ref ID。 |
rate_limited | 429 | 来自此 IP 地址的请求过多 | 放慢速度并重试。每秒请求次数上限为 20 次。 |
server_error | 500 | 意外的内部错误 | 几秒钟后重试。如果问题仍然存在,请联系技术支持。 ref 如有。 |
处理代码中的错误
recommended error handling
const result = await fetch('https://api.tronnrg.com/delegate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ tx_hash: hash, delegate_to: addr, signature: sig }),
}).then(r => r.json());
if (result.error) {
switch (result.error) {
case 'payment_verification_failed':
// Most common: tx not yet indexed. Wait 3s and retry once.
await new Promise(r => setTimeout(r, 3000));
return retry(hash, addr);
case 'hash_already_used':
// Already claimed. Don't retry.
throw new Error('Duplicate delegation attempt');
case 'signature_mismatch':
// Signer != payment sender. Sign with the same key.
throw new Error('Signer does not match payment sender');
case 'delegation_failed':
// Refund queued automatically if payment was verified.
if (result.refund) console.log('Refund queued:', result.refund);
break;
default:
console.error(result.error, result.message);
}
return;
}
// Success
console.log('Delegated:', result.energy, 'energy');
console.log('Delegation tx:', result.delegations[0].tx); // verify on TronScan
console.log('Ref:', result.ref);