Created
February 19, 2019 19:00
-
-
Save 2garryn/257374ee654e98f152cc0c39c65b6a61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Newtonsoft.Json; | |
| namespace Atrades.WebAPI.ApiResponse | |
| { | |
| public class Response | |
| { | |
| private Response(bool error, int code, string message, object data = null) | |
| { | |
| IsError = error; | |
| Code = code; | |
| Message = message; | |
| Data = data; | |
| } | |
| public static Response Success(object data = null) | |
| { | |
| return new Response(false, 0, "success", data); | |
| } | |
| public static Response Error(int code, string message) | |
| { | |
| return new Response(true, code, message); | |
| } | |
| public static void Raise (int code, string message) | |
| { | |
| var resp = Error(code, message); | |
| throw new ResponseException(resp); | |
| } | |
| public override string ToString() | |
| { | |
| return JsonConvert.SerializeObject(this); | |
| } | |
| public bool IsError { get; } | |
| public object Data { get; } | |
| public string Message { get; } | |
| public int Code { get; } | |
| } | |
| public class ResponseException : System.Exception | |
| { | |
| public ResponseException(Response error) : base(error.ToString()) | |
| { | |
| Error = error; | |
| } | |
| public Response Error { get; } | |
| } | |
| public static class Codes | |
| { | |
| public const int InternalError = 1; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment