Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 44c99c5

Browse files
problem details for errors
1 parent b4cadc0 commit 44c99c5

2 files changed

Lines changed: 100 additions & 6 deletions

File tree

src/SqlStreamStore.HAL.Tests/StreamAppendTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,77 @@ public async Task append_expected_version_no_stream()
122122
JToken.DeepEquals(JObject.Parse(page.Messages[0].JsonMetadata), jsonMetadata).ShouldBeTrue();
123123
}
124124

125+
[Theory]
126+
[InlineData(new[]{ExpectedVersion.NoStream, ExpectedVersion.NoStream})]
127+
[InlineData(new[]{ExpectedVersion.NoStream, 2})]
128+
public async Task wrong_expected_version(int[] expectedVersions)
129+
{
130+
var jsonData = JObject.FromObject(new
131+
{
132+
property = "value"
133+
});
134+
135+
var jsonMetadata = JObject.FromObject(new
136+
{
137+
property = "metaValue"
138+
});
139+
140+
for(var i = 0; i < expectedVersions.Length - 1; i++)
141+
{
142+
using(await _fixture.HttpClient.PostAsync(
143+
"/streams/a-stream",
144+
new StringContent(JObject.FromObject(new
145+
{
146+
expectedVersion = expectedVersions[i],
147+
messages = new[]
148+
{
149+
new
150+
{
151+
messageId = Guid.NewGuid(),
152+
type = "type",
153+
jsonData,
154+
jsonMetadata
155+
}
156+
}
157+
}).ToString())
158+
{
159+
Headers =
160+
{
161+
ContentType = new MediaTypeHeaderValue("application/json")
162+
}
163+
}))
164+
{ }
165+
}
166+
167+
using(var response = await _fixture.HttpClient.PostAsync(
168+
"/streams/a-stream",
169+
new StringContent(JObject.FromObject(new
170+
{
171+
expectedVersion = expectedVersions[expectedVersions.Length-1],
172+
messages = new[]
173+
{
174+
new
175+
{
176+
messageId = Guid.NewGuid(),
177+
type = "type",
178+
jsonData,
179+
jsonMetadata
180+
}
181+
}
182+
}).ToString())
183+
{
184+
Headers =
185+
{
186+
ContentType = new MediaTypeHeaderValue("application/json")
187+
}
188+
}))
189+
{
190+
response.StatusCode.ShouldBe(HttpStatusCode.Conflict);
191+
response.Content.Headers.ContentType.ShouldBe(new MediaTypeHeaderValue("application/problem+json"));
192+
}
193+
194+
}
195+
125196
public void Dispose() => _fixture.Dispose();
126197
}
127198
}

src/SqlStreamStore.HAL/AppendStreamMiddleware.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
namespace SqlStreamStore.HAL
22
{
3+
using System;
34
using Microsoft.Owin;
45
using Microsoft.Owin.Builder;
6+
using Newtonsoft.Json.Linq;
57
using Owin;
8+
using SqlStreamStore.Streams;
69
using MidFunc = System.Func<System.Func<System.Collections.Generic.IDictionary<string, object>,
710
System.Threading.Tasks.Task
811
>, System.Func<System.Collections.Generic.IDictionary<string, object>,
@@ -35,16 +38,36 @@ private static MidFunc AppendStream(StreamResource stream) => next => async env
3538

3639
var options = await AppendStreamOptions.Create(context.Request, context.Request.CallCancelled);
3740

38-
var response = await stream.AppendMessages(options, context.Request.CallCancelled);
39-
40-
if(response.StatusCode == 201)
41+
try
4142
{
42-
context.Response.ReasonPhrase = "Created";
43-
context.Response.Headers["Location"] = $"streams/{options.StreamId}";
43+
var response = await stream.AppendMessages(options, context.Request.CallCancelled);
44+
45+
if(response.StatusCode == 201)
46+
{
47+
context.Response.ReasonPhrase = "Created";
48+
context.Response.Headers["Location"] = $"streams/{options.StreamId}";
49+
}
50+
51+
await context.WriteHalResponse(response);
52+
4453
}
54+
catch(WrongExpectedVersionException ex)
55+
{
56+
context.Response.StatusCode = 409;
57+
context.Response.ReasonPhrase = "Conflict";
58+
context.Response.ContentType = "application/problem+json";
4559

46-
await context.WriteHalResponse(response);
60+
await context.Response.WriteAsync(ConvertToProblemDetails(ex));
61+
}
4762
};
4863

64+
private static string ConvertToProblemDetails(Exception ex)
65+
=> JObject.FromObject(new
66+
{
67+
type = "WrongExpectedVersion",
68+
title = "Wrong expected version.",
69+
detail = ex.Message
70+
}).ToString();
71+
4972
}
5073
}

0 commit comments

Comments
 (0)