Exemplo n.º 1
0
        private static void ReturnError(Exception ex, HttpResponse response)
        {
            Log.Debug(ex, "Api Exception");

            try
            {
                JsonError jsonError = null;

                switch (ex)
                {
                case CommandException cex:
                    jsonError = ReturnCommandError(cex, response);
                    break;

                case NotImplementedException _:
                    response.StatusCode = (int)HttpStatusCode.NotImplemented;
                    break;

                case EntityTooLargeException _:
                    response.StatusCode = (int)HttpStatusCode.RequestEntityTooLarge;
                    break;

                default:
                    Log.Error(ex, "Unexpected command error");
                    response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    break;
                }

                jsonError = jsonError ?? new JsonError(ex.Message, CommandExceptionReason.Unknown);
                using (var responseStream = new StreamWriter(response.Body))
                    responseStream.Write(jsonError.Serialize());
            }
            catch (Exception htex) { Log.Warn(htex, "Failed to respond to HTTP request."); }
        }
Exemplo n.º 2
0
        private static void ReturnError(CommandException ex, HttpListenerResponse response)
        {
            var jsonError = new JsonError(ex.Message, ex.Reason);

            switch (ex.Reason)
            {
            case CommandExceptionReason.Unknown:
            case CommandExceptionReason.InternalError:
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return;

            case CommandExceptionReason.Unauthorized:
                response.StatusCode = (int)HttpStatusCode.Unauthorized;
                break;

            case CommandExceptionReason.MissingRights:
                jsonError.HelpLink  = "https://github.com/Splamy/TS3AudioBot/wiki/FAQ#missing-rights";
                response.StatusCode = (int)HttpStatusCode.Forbidden;
                break;

            case CommandExceptionReason.AmbiguousCall:
            case CommandExceptionReason.MissingParameter:
            case CommandExceptionReason.NotSupported:
                response.StatusCode = (int)HttpStatusCode.BadRequest;
                break;

            case CommandExceptionReason.MissingContext:
                if (ex is MissingContextCommandException mcex)
                {
                    if (mcex.MissingType == typeof(InvokerData))
                    {
                        jsonError.HelpMessage += "You have to authenticate yourself to call this method.";
                        jsonError.HelpLink     = "https://github.com/Splamy/TS3AudioBot/wiki/WebAPI#authentication";
                    }
                    else if (mcex.MissingType == typeof(UserSession))
                    {
                        jsonError.HelpMessage += "Creating UserSessions via api is currently not implemented yet.";
                    }
                    else if (mcex.MissingType == typeof(Bot) || mcex.MissingType == typeof(IPlayerConnection) ||
                             mcex.MissingType == typeof(PlayManager) || mcex.MissingType == typeof(Ts3Client) ||
                             mcex.MissingType == typeof(IVoiceTarget) || mcex.MissingType == typeof(IVoiceTarget))
                    {
                        jsonError.HelpMessage += "You are trying to call a command which is specific to a bot. " +
                                                 "Use '!bot use' to switch to a bot instance";
                        jsonError.HelpLink = "https://github.com/Splamy/TS3AudioBot/wiki/FAQ#api-missing-context";
                    }
                }
                goto case CommandExceptionReason.CommandError;

            case CommandExceptionReason.CommandError:
            case CommandExceptionReason.NoReturnMatch:
                response.StatusCode = 422;                 // Unprocessable Entity
                break;

            case CommandExceptionReason.FunctionNotFound:
                response.StatusCode = (int)HttpStatusCode.NotFound;
                break;

            default:
                throw Util.UnhandledDefault(ex.Reason);
            }

            using (var responseStream = new StreamWriter(response.OutputStream))
                responseStream.Write(JsonConvert.SerializeObject(jsonError, ErrorSerializeSettings));
        }
Exemplo n.º 3
0
        private static JsonError ReturnCommandError(CommandException ex, HttpResponse response)
        {
            var jsonError = new JsonError(ex.Message, ex.Reason);

            switch (ex.Reason)
            {
            case CommandExceptionReason.Unknown:
            case CommandExceptionReason.InternalError:
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                break;

            case CommandExceptionReason.Unauthorized:
                jsonError.HelpMessage += "You have to authenticate yourself to call this method.";
                jsonError.HelpLink     = "https://github.com/Splamy/TS3AudioBot/wiki/WebAPI#authentication";
                response.StatusCode    = (int)HttpStatusCode.Unauthorized;
                break;

            case CommandExceptionReason.MissingRights:
                jsonError.HelpLink  = "https://github.com/Splamy/TS3AudioBot/wiki/FAQ#missing-rights";
                response.StatusCode = (int)HttpStatusCode.Forbidden;
                break;

            case CommandExceptionReason.AmbiguousCall:
            case CommandExceptionReason.MissingParameter:
            case CommandExceptionReason.NotSupported:
                response.StatusCode = (int)HttpStatusCode.BadRequest;
                break;

            case CommandExceptionReason.MissingContext:
                if (ex is MissingContextCommandException mcex)
                {
                    if (mcex.MissingType == typeof(ClientCall))
                    {
                        jsonError.HelpMessage += strings.error_not_available_from_api;
                    }
                    else if (mcex.MissingType == typeof(UserSession))
                    {
                        jsonError.HelpMessage += "Creating UserSessions via api is currently not implemented yet.";
                    }
                    else if (mcex.MissingType == typeof(Bot) || mcex.MissingType == typeof(Player) ||
                             mcex.MissingType == typeof(PlayManager) || mcex.MissingType == typeof(Ts3Client) ||
                             mcex.MissingType == typeof(IVoiceTarget) || mcex.MissingType == typeof(IVoiceTarget) ||
                             mcex.MissingType == typeof(ConfBot))
                    {
                        jsonError.HelpMessage += "You are trying to call a command which is specific to a bot. " +
                                                 "Use '!bot use' to switch to a bot instance";
                        jsonError.HelpLink = "https://github.com/Splamy/TS3AudioBot/wiki/FAQ#api-missing-context";
                    }
                }
                goto case CommandExceptionReason.CommandError;

            case CommandExceptionReason.CommandError:
            case CommandExceptionReason.NoReturnMatch:
                response.StatusCode = 422;                 // Unprocessable Entity
                break;

            case CommandExceptionReason.FunctionNotFound:
                response.StatusCode = (int)HttpStatusCode.NotFound;
                break;

            default:
                throw Tools.UnhandledDefault(ex.Reason);
            }

            return(jsonError);
        }