checkCode static method

void checkCode(
  1. OpenSslFfi lib,
  2. int result, {
  3. String msg = 'Operation failed',
})

Helper to check OpenSSL result codes. Standard OpenSSL convention: 1 is success, 0 is failure (e.g. signature verify), <0 is error.

Implementation

static void checkCode(OpenSslFfi lib, int result, {String msg = 'Operation failed'}) {
  if (result <= 0) {
    // Retrieve the error from OpenSSL error queue
    final errorCode = lib.ERR_get_error();
    if (errorCode != 0) {
      final buffer = calloc<Char>(256);
      try {
        lib.ERR_error_string(errorCode, buffer);
        final errorParams = buffer.cast<Utf8>().toDartString();
        throw Exception('OpenSSL Error: $msg. Code: $result. OpenSSL: $errorParams');
      } finally {
        calloc.free(buffer);
      }
    }
    // If no error code in queue, just throw generic
    throw Exception('OpenSSL Error: $msg (Code: $result) - No detail in error queue');
  }
}