Skip to content

Commit

Permalink
fix Bug 53455
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbannov committed Dec 13, 2021
1 parent 0d577d4 commit 17d2c04
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
32 changes: 14 additions & 18 deletions common/ASC.Api.Core/Middleware/CommonApiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,16 @@ protected CommonApiResponse(HttpStatusCode statusCode)
{
StatusCode = statusCode;
}

public static SuccessApiResponse Create(HttpStatusCode statusCode, object response)
{
return new SuccessApiResponse(statusCode, response);
}

public static ErrorApiResponse CreateError(HttpStatusCode statusCode, Exception error)
{
return new ErrorApiResponse(statusCode, error);
}
}

public class ErrorApiResponse : CommonApiResponse
{
public CommonApiError Error { get; set; }

protected internal ErrorApiResponse(HttpStatusCode statusCode, Exception error, string message = null) : base(statusCode)
protected internal ErrorApiResponse(HttpStatusCode statusCode, Exception error, string message, bool withStackTrace) : base(statusCode)
{
Status = 1;
Error = CommonApiError.FromException(error, message);
Error = CommonApiError.FromException(error, message, withStackTrace);
}
}

Expand Down Expand Up @@ -88,15 +78,21 @@ public class CommonApiError

public int Hresult { get; set; }

public static CommonApiError FromException(Exception exception, string message = null)
public static CommonApiError FromException(Exception exception, string message, bool withStackTrace)
{
return new CommonApiError()
var result = new CommonApiError()
{
Message = message ?? exception.Message,
Type = exception.GetType().ToString(),
Stack = exception.StackTrace,
Hresult = exception.HResult
Message = message ?? exception.Message
};

if (withStackTrace)
{
result.Type = exception.GetType().ToString();
result.Stack = exception.StackTrace;
result.Hresult = exception.HResult;
}

return result;
}
}
}
11 changes: 9 additions & 2 deletions common/ASC.Api.Core/Middleware/ResponseWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Net;
using System.Security;
using System.Security.Authentication;

using ASC.Common.Web;

Expand All @@ -21,6 +22,8 @@ public override void OnException(ExceptionContext context)
status = HttpStatusCode.InternalServerError;
}

bool withStackTrace = true;

switch (context.Exception)
{
case ItemNotFoundException:
Expand All @@ -35,17 +38,21 @@ public override void OnException(ExceptionContext context)
status = HttpStatusCode.Forbidden;
message = "Access denied";
break;
case AuthenticationException:
status = HttpStatusCode.Unauthorized;
withStackTrace = false;
break;
case InvalidOperationException:
status = HttpStatusCode.Forbidden;
break;
}

var result = new ObjectResult(new ErrorApiResponse(status, context.Exception, message))
var result = new ObjectResult(new ErrorApiResponse(status, context.Exception, message, withStackTrace))
{
StatusCode = (int)status
};

context.Result = result; ;
context.Result = result;
}
}

Expand Down

0 comments on commit 17d2c04

Please sign in to comment.