As of #62388 it is now possible to set custom HTTP Status Codes instead of always returning 200.

To maintain compatibility with existing logic/documentation, the change does not affect already implemented service endpoints.

To set a custom HTTP Status Code you need to use the newly created One.Core.Business.ServiceObjectResult that extends the popular One.Core.Business.ObjectResult.

If you are updating existing logic, you will need to pass your old object via ServiceObjectResult.Data and set the ServiceObjectResult.StatusCode and ServiceObjectResult.BusinessState.

Below you can see some examples:

Old logic

public string StringReturningMethod()
{
	var myData = "Test String";
	return myData;
}

If not given, StatusCode defaults to System.Net.HttpStatusCode.OK (200) and BusinessState to BusinessStatuses.BusinessOk

New Logic

public ServiceObjectResult<object> StringReturningMethod()
{
	var myData = "Test String";
	return new ServiceObjectResult<object>
	{
		Data = myData
	}
}
public void ErrorHandlingExampleMethod()
{
	SetError(new BusinessError()
	{ 
		BusinessErrorType = BusinessErrorTypes.Error,
		BusinessErrorCode = BusinessErrors.InterfaceError,
		BusinessErrorDescription = "Error Description"
	});
}

IMPORTANT

Do NOT use the One.Core.Business.BusinessCaseBaseManager.SetError() method to register errors. By doing so, you will bypass the new implementation (even if you return a ServiceObjectResult eventually) and the old 200 response will be returned. Again, this is necessary to maintain compatibility with old logic.


This also means that you have to ensure you change all existing SetError entries of your method to returning ServiceObjectResult

public ServiceObjectResult<object> ErrorHandlingExampleMethod()
{
	SetError(new BusinessError()
	{ 
		BusinessErrorType = BusinessErrorTypes.Error,
		BusinessErrorCode = BusinessErrors.InterfaceError,
		BusinessErrorDescription = "Error Description"
	});
	return new ServiceObjectResult<object>
	{
		StatusCode = System.Net.HttpStatusCode.BadRequest,
        BusinessState = new One.Core.Business.BusinessState
		{
			BusinessStatus = One.Core.Business.Interfaces.BusinessStatuses.BusinessFailed,
			BusinessStatusName = One.Core.Business.Interfaces.BusinessStatuses.BusinessFailed.ToString(),
			BusinessErrors = new List<BusinessError> {new BusinessError()
			{ 
				BusinessErrorType = BusinessErrorTypes.Error,
				BusinessErrorCode = BusinessErrors.InterfaceError,
				BusinessErrorDescription = "Error Description"
			} }
		}
	}
}
public MyClass ObjectReturningMethod()
{
	var myData = new MyClass();
	return myData;
}
public ServiceObjectResult<object> ObjectReturningMethod()
{
	var myData = new MyClass();
	return new ServiceObjectResult<object>
	{
		StatusCode = System.Net.HttpStatusCode.OK,
        BusinessState = new One.Core.Business.BusinessState
		{
			BusinessStatus = One.Core.Business.Interfaces.BusinessStatuses.BusinessOk
			BusinessStatusName = One.Core.Business.Interfaces.BusinessStatuses.BusinessOk.ToString()
		},
        Data = myData
	}
}

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.