{"record":{"id":"7466960b0f0bb4be","repo":"babalae/better-genshin-impact","slug":"error-sending-telegram-notification-ex-message","errorCode":null,"errorMessage":"Error sending Telegram notification: {ex.Message}","messagePattern":"Error sending Telegram notification: (.+?)","errorType":"exception","errorClass":"NotifierException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Service/Notifier/TelegramNotifier.cs","lineNumber":138,"sourceCode":"            if (content.Screenshot != null)\n            {\n                await SendImageMessageAsync(content.Screenshot, content.Message.Length < 1024 ? content.Message : null);\n                if (content.Message.Length < 1024) return;\n            }\n\n            await SendTextMessageAsync(content.Message);\n        }\n        catch (HttpRequestException ex)\n        {\n            throw new NotifierException(\"Network error sending Telegram notification: \" + ex.Message);\n        }\n        catch (TaskCanceledException)\n        {\n            throw new NotifierException(\"Telegram API request timed out. Check your internet connection.\");\n        }\n        catch (System.Exception ex) when (ex is not NotifierException)\n        {\n            throw new NotifierException(\"Error sending Telegram notification: \" + ex.Message);\n        }\n    }\n\n    private async Task SendImageMessageAsync(Image<Rgb24> image, string? caption)\n    {\n        var endpoint = $\"{TelegramApiBaseUrl}{TelegramBotToken}/sendPhoto\";\n        using var memoryStream = new MemoryStream();\n        await image.SaveAsPngAsync(memoryStream);\n        memoryStream.Position = 0;\n\n        var content = new MultipartFormDataContent\n        {\n            { new StreamContent(memoryStream), \"photo\", \"image.png\" },\n            { new StringContent(TelegramChatId), \"chat_id\" }\n        };\n        if (!string.IsNullOrEmpty(caption)) content.Add(new StringContent(caption), \"caption\");\n\n        await SendRequestAsync(endpoint, content, \"image\");","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Service/Notifier/TelegramNotifier.cs#L120-L156","documentation":"Final catch-all for any exception inside SendAsync that is not NotifierException, HttpRequestException, or TaskCanceledException (the `when (ex is not NotifierException)` filter). Covers InvalidOperationException, JsonException from ValidateApiResponse's caller path, ObjectDisposedException, ArgumentException, etc.","triggerScenarios":"An unexpected exception type escapes the inner try — e.g. JsonException if SendRequestAsync's response is non-JSON and ValidateApiResponse's own try/catch is bypassed, or ObjectDisposedException if the notifier is used after Dispose.","commonSituations":"Telegram API returns HTML (proxy error page) breaking JSON parse; the notifier disposed but still referenced; an unforeseen framework exception.","solutions":["Preserve ex as InnerException for stack-trace diagnosis.","Add a dedicated JsonException catch with a clearer 'non-JSON Telegram response' message.","Guard against use-after-Dispose of the notifier.","Log the full exception type name in the message."],"exampleFix":"// before\ncatch (System.Exception ex) when (ex is not NotifierException)\n{\n    throw new NotifierException(\"Error sending Telegram notification: \" + ex.Message);\n}\n\n// after\ncatch (JsonException ex)\n{\n    throw new NotifierException(\"Telegram returned a non-JSON response: \" + ex.Message, ex);\n}\ncatch (System.Exception ex) when (ex is not NotifierException)\n{\n    throw new NotifierException($\"Error sending Telegram notification ({ex.GetType().Name}): {ex.Message}\", ex);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await telegramNotifier.SendAsync(data); }\ncatch (NotifierException ex) when (ex.Message.StartsWith(\"Error sending Telegram\"))\n{ /* unexpected — log full type + stack, do not silently retry */ }","preventionTips":["Add a dedicated JsonException catch for non-JSON responses.","Guard against use-after-Dispose of the notifier.","Include the exception type name and preserve InnerException."],"tags":["telegram","catch-all","notifier","observability"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}