The Akatus .NET SDK uses the RestExcepction class (note the spelling) to handle API errors. Proper error handling ensures your application can gracefully manage failed requests and provide meaningful feedback to users.
The request was malformed or contains invalid data.Common Causes:
Missing required fields
Invalid field formats (e.g., invalid credit card number)
Invalid enum values
Incorrect data types
Example:
catch (RestExcepction ex){ if (ex.StatusCode == 400) { Console.WriteLine("Invalid request data:"); foreach (string error in ex.ErrorMessages) { Console.WriteLine($" - {error}"); // Log specific validation errors if (error.Contains("cartao")) { LogError("Invalid credit card information"); } } }}
401 - Unauthorized
Authentication failed. The API key is invalid or missing.Common Causes:
Invalid API key in Web.config
Missing API key configuration
Using test API key in production (or vice versa)
Example:
catch (RestExcepction ex){ if (ex.StatusCode == 401) { LogError("Authentication failed. Check API key configuration."); // Verify configuration string apiKey = ConfigurationManager.AppSettings["AkatusApiKey"]; if (string.IsNullOrEmpty(apiKey)) { Console.WriteLine("API Key is not configured!"); } }}
403 - Forbidden
The request is valid, but you don’t have permission to perform this action.Common Causes:
Payment method not enabled in your account
Account limitations or restrictions
Trying to access resources you don’t own
Example:
catch (RestExcepction ex){ if (ex.StatusCode == 403) { Console.WriteLine("Access denied. Check account permissions."); // Check if payment method is enabled ShowUserMessage("This payment method is not available. Please contact support."); }}
404 - Not Found
The requested resource doesn’t exist.Common Causes:
Invalid transaction ID in status queries
Querying transactions from wrong environment (test vs production)
Example:
catch (RestExcepction ex){ if (ex.StatusCode == 404) { Console.WriteLine($"Transaction not found."); // Verify environment var ambiente = Config.Ambiente; Console.WriteLine($"Current environment: {ambiente}"); }}
500 - Internal Server Error
The Akatus server encountered an error.Common Causes:
Temporary API outage
Akatus server issues
Example:
catch (RestExcepction ex){ if (ex.StatusCode == 500) { LogError("Akatus server error. Will retry."); // Implement retry logic Thread.Sleep(5000); // Retry the request }}
503 - Service Unavailable
The API is temporarily unavailable.Common Causes:
Scheduled maintenance
High traffic periods
Example:
catch (RestExcepction ex){ if (ex.StatusCode == 503) { Console.WriteLine("Service temporarily unavailable."); ShowUserMessage("Payment system is under maintenance. Please try again later."); }}
Translate technical errors into user-friendly messages:
public class ErrorMessageTranslator{ public string GetUserFriendlyMessage(RestExcepction ex) { // Check for specific error messages if (ex.ErrorMessages != null) { foreach (string error in ex.ErrorMessages) { if (error.Contains("cartão inválido") || error.Contains("invalid card")) return "The credit card information is invalid. Please check the card number and try again."; if (error.Contains("CPF")) return "The CPF number is invalid. Please verify and try again."; if (error.Contains("expirado") || error.Contains("expired")) return "This credit card has expired. Please use a different card."; if (error.Contains("saldo") || error.Contains("insufficient")) return "The transaction was declined due to insufficient funds."; } } // Generic messages based on status code switch (ex.StatusCode) { case 400: return "There was a problem with your payment information. Please review and try again."; case 401: return "We're experiencing technical difficulties. Please try again later or contact support."; case 403: return "This payment method is not available. Please select a different option."; case 500: case 503: return "Our payment system is temporarily unavailable. Please try again in a few minutes."; default: return "An error occurred while processing your payment. Please try again or contact support."; } }}
Validate data before making API calls to catch errors early:
public class CartValidator{ public List<string> ValidateCart(Carrinho carrinho) { var errors = new List<string>(); // Validate customer info if (string.IsNullOrEmpty(carrinho.Pagador.Nome)) errors.Add("Customer name is required"); if (string.IsNullOrEmpty(carrinho.Pagador.Email)) errors.Add("Customer email is required"); else if (!IsValidEmail(carrinho.Pagador.Email)) errors.Add("Customer email is invalid"); // Validate products if (carrinho.Produtos == null || carrinho.Produtos.Count == 0) errors.Add("At least one product is required"); foreach (var produto in carrinho.Produtos) { if (produto.Preco <= 0) errors.Add($"Product {produto.Descricao} has invalid price"); if (produto.Quantidade <= 0) errors.Add($"Product {produto.Descricao} has invalid quantity"); } // Validate credit card if applicable if (IsCartaoPayment(carrinho.Transacao.MeioDePagamento)) { if (string.IsNullOrEmpty(carrinho.Transacao.Cartao.Numero)) errors.Add("Card number is required"); if (string.IsNullOrEmpty(carrinho.Transacao.Cartao.CodigoSeguranca)) errors.Add("Security code is required"); if (string.IsNullOrEmpty(carrinho.Transacao.Cartao.Expiracao)) errors.Add("Card expiration is required"); } return errors; } private bool IsValidEmail(string email) { try { var addr = new System.Net.Mail.MailAddress(email); return addr.Address == email; } catch { return false; } } private bool IsCartaoPayment(Akatus.Enums.MeioDePagamento meio) { return meio.ToString().StartsWith("cartao_"); }}