using static MFiles.Grpc.Samples.MFilesValues; namespace MFiles.Grpc.Samples.Recipes; /// /// Recipe "errors": provoke "not found" and "access denied" and show the decoded M-Files errors. /// public static class Errors { // M-Files error codes this recipe checks for. private const uint NotFound = 0x8004000B; private const uint AccessDenied = 0x800407E4; public static async Task RunAsync(MFilesSettings settings, string[] args) { await using var connection = await MFilesConnection.ConnectAsync(settings, RecipeHelpers.ClientName); // [START errors] // Not found: a document ID that does not exist. var objects = new IRPCObjectOperations.IRPCObjectOperationsClient(connection.CallInvoker); try { await objects.GetObjectInfoAndPropertiesAsync(new GetObjectInfoAndPropertiesRequest { ObjId = ObjId(0, 999_999_999), LatestVersion = true, }); throw new InvalidOperationException("Expected the call to fail."); } catch (MFilesException e) when (e.Has(NotFound)) { Print("Not found", e); } // Access denied: an administrative call by a user who is not a vault administrator. var admin = new IRPCPropertyDefsAdmin.IRPCPropertyDefsAdminClient(connection.CallInvoker); try { await admin.GetPropertyDefsAdminAsync(new GetPropertyDefsAdminRequest()); Console.WriteLine("Access denied: not provoked, the user is a vault administrator"); } catch (MFilesException e) when (e.Has(AccessDenied)) { Print("Access denied", e); } // [END errors] } // [START print-error] private static void Print(string label, MFilesException e) { Console.WriteLine($"{label}: gRPC status {e.StatusCode}, M-Files error {e.ErrorCodeHex} ({e.ErrorCode})"); Console.WriteLine($" Message: {e.Message.ReplaceLineEndings(" ")}"); Console.WriteLine($" Error stack, original error first: " + string.Join(", ", e.ErrorCodes.Distinct().Select(code => $"0x{code:X8}"))); } // [END print-error] }