2022-12-14 14:51:32 +00:00
|
|
|
using System.Net;
|
2020-02-12 15:16:19 +01:00
|
|
|
using System.Net.Sockets;
|
2025-07-16 11:48:48 -04:00
|
|
|
using System.Globalization;
|
2021-01-31 15:03:11 +01:00
|
|
|
using Myriad.Rest.Exceptions;
|
2025-07-16 11:48:48 -04:00
|
|
|
using Myriad.Rest.Types;
|
2020-05-05 19:09:18 +02:00
|
|
|
|
2020-05-07 23:59:05 +02:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
2020-03-08 10:55:33 +01:00
|
|
|
using Npgsql;
|
|
|
|
|
|
2020-02-12 15:16:19 +01:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
2021-03-18 20:16:28 +01:00
|
|
|
using Polly.Timeout;
|
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
|
|
public static class MiscUtils
|
2020-02-12 15:16:19 +01:00
|
|
|
{
|
2021-11-26 21:10:56 -05:00
|
|
|
public static string ProxyTagsString(this PKMember member, string separator = ", ") =>
|
|
|
|
|
string.Join(separator, member.ProxyTags.Select(t => t.ProxyString.AsCode()));
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
public static bool IsOurProblem(this Exception e)
|
|
|
|
|
{
|
|
|
|
|
// This function filters out sporadic errors out of our control from being reported to Sentry
|
|
|
|
|
// otherwise we'd blow out our error reporting budget as soon as Discord takes a dump, or something.
|
2020-05-07 23:59:05 +02:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// Occasionally Discord's API will Have A Bad Time and return a bunch of CloudFlare errors (in HTML format).
|
|
|
|
|
// The library tries to parse these HTML responses as JSON and crashes with a consistent exception message.
|
|
|
|
|
if (e is JsonReaderException jre && jre.Message ==
|
|
|
|
|
"Unexpected character encountered while parsing value: <. Path '', line 0, position 0.") return false;
|
2020-05-12 22:19:33 +02:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// And now (2020-05-12), apparently Discord returns these weird responses occasionally. Also not our problem.
|
|
|
|
|
if (e is BadRequestException bre && bre.ResponseBody.Contains("<center>nginx</center>")) return false;
|
|
|
|
|
if (e is NotFoundException ne && ne.ResponseBody.Contains("<center>nginx</center>")) return false;
|
|
|
|
|
if (e is UnauthorizedException ue && ue.ResponseBody.Contains("<center>nginx</center>")) return false;
|
2020-11-16 09:05:00 +01:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// Filter out timeout/ratelimit related stuff
|
|
|
|
|
if (e is TooManyRequestsException) return false;
|
|
|
|
|
if (e is RatelimitBucketExhaustedException) return false;
|
|
|
|
|
if (e is TimeoutRejectedException) return false;
|
2021-03-18 20:16:28 +01:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// 5xxs? also not our problem :^)
|
|
|
|
|
if (e is UnknownDiscordRequestException udre && (int)udre.StatusCode >= 500) return false;
|
2020-11-16 09:05:00 +01:00
|
|
|
|
2022-12-14 14:51:32 +00:00
|
|
|
// 409s apparently happen for Discord internal issues.
|
|
|
|
|
if (e is UnknownDiscordRequestException udre2 && udre2.StatusCode == HttpStatusCode.Conflict) return false;
|
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// Webhook server errors are also *not our problem*
|
|
|
|
|
// (this includes rate limit errors, WebhookRateLimited is a subclass)
|
|
|
|
|
if (e is WebhookExecutionErrorOnDiscordsEnd) return false;
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// Socket errors are *not our problem*
|
2024-10-18 03:34:10 +09:00
|
|
|
// if (e.GetBaseException() is SocketException) return false;
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// Tasks being cancelled for whatver reason are, you guessed it, also not our problem.
|
2024-10-18 03:34:10 +09:00
|
|
|
// if (e is TaskCanceledException) return false;
|
2020-02-18 21:56:15 +01:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// Sometimes Discord just times everything out.
|
2024-10-18 03:34:10 +09:00
|
|
|
// if (e is TimeoutException) return false;
|
2022-04-20 10:45:35 -04:00
|
|
|
if (e is UnknownDiscordRequestException tde && tde.Message == "Request Timeout") return false;
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// HTTP/2 streams are complicated and break sometimes.
|
2024-10-18 03:34:10 +09:00
|
|
|
// if (e is HttpRequestException) return false;
|
2021-09-29 21:51:54 -04:00
|
|
|
|
2022-12-03 11:57:43 +00:00
|
|
|
// This may expanded at some point.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool ShowToUser(this Exception e)
|
|
|
|
|
{
|
2022-12-14 14:47:08 +00:00
|
|
|
if (e is PostgresException pe)
|
|
|
|
|
{
|
|
|
|
|
// ignore "cached plan must not change result type" error
|
|
|
|
|
if (pe.SqlState == "0A000") return false;
|
2022-12-03 11:57:43 +00:00
|
|
|
|
2022-12-14 14:47:08 +00:00
|
|
|
// Ignore "Database is shutting down" error
|
|
|
|
|
if (pe.SqlState == "57P03") return false;
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2022-12-14 14:47:08 +00:00
|
|
|
// Ignore *other* "database is shutting down" error (57P01)
|
|
|
|
|
if (pe.SqlState == "57P01") return false;
|
2022-03-19 21:57:58 -04:00
|
|
|
|
2022-12-14 14:47:08 +00:00
|
|
|
// ignore "out of shared memory" error
|
|
|
|
|
if (pe.SqlState == "53200") return false;
|
|
|
|
|
|
|
|
|
|
// ignore "too many clients already" error
|
|
|
|
|
if (pe.SqlState == "53300") return false;
|
|
|
|
|
}
|
2022-12-10 16:50:43 +00:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// Ignore database timing out as well.
|
|
|
|
|
if (e is NpgsqlException tpe && tpe.InnerException is TimeoutException)
|
|
|
|
|
return false;
|
2021-11-09 01:31:42 -05:00
|
|
|
|
2024-11-08 06:50:02 +09:00
|
|
|
if (e is NpgsqlException npe &&
|
|
|
|
|
(
|
|
|
|
|
// Ignore thread pool exhaustion errors
|
|
|
|
|
npe.Message.Contains("The connection pool has been exhausted")
|
|
|
|
|
// ignore "Exception while reading from stream"
|
|
|
|
|
|| npe.Message.Contains("Exception while reading from stream")
|
|
|
|
|
// ignore "Exception while connecting"
|
|
|
|
|
|| npe.Message.Contains("Exception while connecting")
|
|
|
|
|
))
|
2022-12-03 11:57:43 +00:00
|
|
|
return false;
|
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
return true;
|
2020-02-12 15:16:19 +01:00
|
|
|
}
|
2025-07-16 11:48:48 -04:00
|
|
|
|
|
|
|
|
public static MultipartFile GenerateColorPreview(string color)
|
|
|
|
|
{
|
|
|
|
|
//generate a 128x128 solid color gif from bytes
|
|
|
|
|
//image data is a 1x1 pixel, using the background color to fill the rest of the canvas
|
|
|
|
|
var imgBytes = new byte[]
|
|
|
|
|
{
|
|
|
|
|
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, // Header
|
|
|
|
|
0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, // Logical Screen Descriptor
|
|
|
|
|
0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, // Global Color Table
|
|
|
|
|
0x21, 0xF9, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, // Graphics Control Extension
|
|
|
|
|
0x2C, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, // Image Descriptor
|
|
|
|
|
0x02, 0x02, 0x4C, 0x01, 0x00, // Image Data
|
|
|
|
|
0x3B // Trailer
|
|
|
|
|
}; //indices 13, 14 and 15 are the R, G, and B values respectively
|
|
|
|
|
|
|
|
|
|
imgBytes[13] = byte.Parse(color.Substring(0, 2), NumberStyles.HexNumber);
|
|
|
|
|
imgBytes[14] = byte.Parse(color.Substring(2, 2), NumberStyles.HexNumber);
|
|
|
|
|
imgBytes[15] = byte.Parse(color.Substring(4, 2), NumberStyles.HexNumber);
|
|
|
|
|
|
|
|
|
|
return new MultipartFile("color.gif", new MemoryStream(imgBytes), null, null, null);
|
|
|
|
|
}
|
2020-02-12 15:16:19 +01:00
|
|
|
}
|