internal static ImageflowException FromContext(JobContextHandle c, ulong defaultBufferSize = 2048) { var result = ErrorFetchResult.BufferTooSmall; for (var bufferSize = defaultBufferSize; bufferSize < MaxBufferSize; bufferSize *= 2) { result = TryGetErrorString(c, bufferSize, out var message); switch (result) { case ErrorFetchResult.Success: return(new ImageflowException(message)); case ErrorFetchResult.ContextInvalid: return(null); case ErrorFetchResult.NoError: return(null); case ErrorFetchResult.BufferTooSmall: break; default: throw new ArgumentOutOfRangeException(); } } if (result == ErrorFetchResult.BufferTooSmall) { throw new ImageflowAssertionFailed( $"Imageflow error and stacktrace exceeded {MaxBufferSize} bytes"); } return(null); }
private static ErrorFetchResult TryGetErrorString(JobContextHandle c, ulong bufferSize, out string message) { message = null; if (c.IsClosed || c.IsInvalid) { return(ErrorFetchResult.ContextInvalid); } if (!NativeMethods.imageflow_context_has_error(c)) { return(ErrorFetchResult.NoError); } var buffer = new byte[bufferSize]; var pinned = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { var everythingWritten = NativeMethods.imageflow_context_error_write_to_buffer(c, pinned.AddrOfPinnedObject(), new UIntPtr((ulong)buffer.LongLength), out var bytesWritten); message = bytesWritten.ToUInt64() > 0 ? Encoding.UTF8.GetString(buffer, 0, (int)Math.Min(bytesWritten.ToUInt64(), bufferSize)) : ""; return(everythingWritten ? ErrorFetchResult.Success : ErrorFetchResult.BufferTooSmall); } finally { pinned.Free(); } }
internal static ImageflowException FromContext(JobContextHandle c, ulong defaultBufferSize = 2048) { if (c.IsClosed || c.IsInvalid || !NativeMethods.imageflow_context_has_error(c)) { return(null); } var buffer = new byte[defaultBufferSize]; var pinned = GCHandle.Alloc(buffer, GCHandleType.Pinned); bool everythingWritten; string message = null; try { everythingWritten = NativeMethods.imageflow_context_error_write_to_buffer(c, pinned.AddrOfPinnedObject(), new UIntPtr((ulong)buffer.LongLength), out var bytesWritten); if (bytesWritten.ToUInt64() > 0) { message = Encoding.UTF8.GetString(buffer, 0, (int)Math.Min(bytesWritten.ToUInt64(), defaultBufferSize)).Replace("\n", ""); message = message + message.Length; Debug.WriteLine(message); Console.WriteLine(message); } } finally { pinned.Free(); } if (everythingWritten) { return(new ImageflowException(message ?? "Empty error and stacktrace")); } if (defaultBufferSize < MaxBufferSize) { return(FromContext(c, MaxBufferSize)); } throw new ImageflowAssertionFailed( $"Imageflow error and stacktrace exceeded {MaxBufferSize} bytes"); }
public static extern bool imageflow_json_response_read(JobContextHandle context, JsonResponseHandle responseIn, out int statusCodeOut, out IntPtr bufferUtf8NoNullsOut, out UIntPtr bufferSizeOut);
public static extern bool imageflow_context_print_and_exit_if_error(JobContextHandle context);
public static extern int imageflow_context_error_as_http_code(JobContextHandle context);
public static extern bool imageflow_context_error_try_clear(JobContextHandle context);
public static extern bool imageflow_json_response_read(JobContextHandle context, JsonResponseHandle response_in, out int status_code_out, out IntPtr buffer_utf8_no_nulls_out, out UIntPtr buffer_size_out);
public static extern bool imageflow_context_error_write_to_buffer(JobContextHandle context, IntPtr buffer, UIntPtr bufferLength, out UIntPtr bytesWritten);
public static extern bool imageflow_context_get_output_buffer_by_id(JobContextHandle context, int ioId, out IntPtr resultBuffer, out UIntPtr resultBufferLength);
public static extern bool imageflow_context_add_output_buffer(JobContextHandle context, int ioId);
public static extern bool imageflow_context_add_input_buffer(JobContextHandle context, int ioId, IntPtr buffer, UIntPtr bufferByteCount, Lifetime lifetime);
public static extern IntPtr imageflow_context_memory_allocate(JobContextHandle context, IntPtr bytes, IntPtr filename, int line);
public static extern bool imageflow_json_response_destroy(JobContextHandle context, IntPtr response);
public static extern bool imageflow_context_begin_terminate(JobContextHandle context);
public static extern bool imageflow_context_memory_free(JobContextHandle context, IntPtr pointer, IntPtr filename, int line);
public static extern bool imageflow_context_has_error(JobContextHandle context);
public static extern IntPtr imageflow_context_send_json(JobContextHandle context, IntPtr method, IntPtr jsonBuffer, UIntPtr jsonBufferSize);
public static extern bool imageflow_context_error_recoverable(JobContextHandle context);
public JobContext() { _handle = new JobContextHandle(); }
public JsonResponseHandle(JobContextHandle parent, IntPtr ptr) : base(true) { ParentContext = parent ?? throw new ArgumentNullException(nameof(parent)); SetHandle(ptr); }