Skip to content
7 changes: 7 additions & 0 deletions Morpher.WebService.V3.Client/BadRequestException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ namespace Morpher.WebService.V3
class BadRequestException : Exception
{
public int Status { get; }
public int ErrorCode { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Все хорошо, только нужно где-то установить значение ErrorCode. Думаю, лучше это сделать в конструкторе BadRequestException.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ок


public BadRequestException(int status, int errorCode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Конструктор не используется, следовательно, свойство ErrorCode всегда будет 0.

{
Status = status;
ErrorCode = errorCode;
}

public BadRequestException(int status)
{
Expand Down
65 changes: 53 additions & 12 deletions Morpher.WebService.V3.Client/Qazaq/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ public string GetCardinal(long n, bool useOne)
{
client.AddParam("n", n.ToString());
client.AddParam("use-one", useOne.ToString());

var stringResult = client.GetObject<string>("/qazaq/cardinal");

return stringResult;
try
{
return client.GetObject<string>("/qazaq/cardinal");
}
catch (BadRequestException e) when (e.ErrorCode == 19)
{
throw new OutOfRangeException(nameof(n));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Какое значение нужно передать в метод GetCardinal, чтобы сервис вернул ответ с кодом 19?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OutOfRangeException будем возвращать если n больше int.Max или меньше int.Min.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем? Сервис умеет строить пропись для всех значений типа long.

}
}
}

Expand All @@ -56,9 +60,14 @@ public string GetOrdinal(string cardinal)
{
client.AddParam("cardinal", cardinal);

var stringResult = client.GetObject<string>("/qazaq/ordinal");

return stringResult;
try
{
return client.GetObject<string>("/qazaq/ordinal");
}
catch (BadRequestException e) when (e.ErrorCode == 15)
{
throw new ArgumentNotQazaqException(nameof(cardinal));
}
}
}

Expand All @@ -69,8 +78,31 @@ public string GetDate(string date, bool useOne)
client.AddParam("date", date);
client.AddParam("use-one", useOne.ToString());
var stringResult = client.GetObject<string>("/qazaq/date");
try
{
return client.GetObject<string>("/qazaq/date");
}
catch (BadRequestException e) when (e.ErrorCode == 16)
{
throw new QazaqWrongDateException(nameof(date));
}
}
}

return stringResult;
public string GetDate(DateTime dateTime, bool useOne)
{
using (var client = _newClient())
{
client.AddParam("date", dateTime.ToString("yyyy-MM-dd"));
client.AddParam("use-one", useOne.ToString());
try
{
return client.GetObject<string>("/qazaq/date");
}
catch (BadRequestException e) when (e.ErrorCode == 16)
{
throw new QazaqWrongDateException(nameof(dateTime));
}
}
}

Expand All @@ -80,10 +112,19 @@ public string GetDayOfMonth(int day, int month)
{
client.AddParam("day", day.ToString());
client.AddParam("month", month.ToString());

var stringResult = client.GetObject<string>("/qazaq/day-of-month");

return stringResult;
try
{
return client.GetObject<string>("/qazaq/day-of-month");
}
catch (BadRequestException e)
{
switch (e.ErrorCode)
{
case 17: throw new QazaqWrongDayOfMonthException(nameof(day));
case 18: throw new QazaqWrongMonthException(nameof(month));
}
throw;
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions Morpher.WebService.V3.Client/Qazaq/OutOfRangeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Morpher.WebService.V3.Qazaq
{
public class OutOfRangeException : InvalidArgumentException
{
public override string Message =>
"Недопустимое значение параметра n." +
" Ожидается целое число в диапазоне +/−2 в 63 степени.";

public OutOfRangeException(string parameterName)
: base(parameterName)
{
}
}
}
13 changes: 13 additions & 0 deletions Morpher.WebService.V3.Client/Qazaq/QazaqWrongDateException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Morpher.WebService.V3.Qazaq
{
public class QazaqWrongDateException : InvalidArgumentException
{
public override string Message =>
"Неправильная дата. Ожидается формат ГГГГ-ММ-ДД.";

public QazaqWrongDateException(string parameterName)
: base(parameterName)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Morpher.WebService.V3.Qazaq
{
public class QazaqWrongMonthException : InvalidArgumentException
{
public override string Message =>
"Недопустимое значение числового параметра month. Допустимы значения от 1 до 12 включительно.";

public QazaqWrongMonthException(string parameterName)
: base(parameterName)
{
}
}
}
13 changes: 13 additions & 0 deletions Morpher.WebService.V3.Client/Qazaq/QazaqWrongMonthException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Morpher.WebService.V3.Qazaq
{
public class QazaqWrongDayOfMonthException : InvalidArgumentException
{
public override string Message =>
"Недопустимое значение числового параметра day. Допустимы значения от 1 до 31 включительно.";

public QazaqWrongDayOfMonthException(string parameterName)
: base(parameterName)
{
}
}
}