Skip to content

Commit 69d6632

Browse files
committed
feat: update docs
1 parent db96726 commit 69d6632

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ As you can see, you need to derive from MiddyNet, and specify the type of event
7070
After your code is executed, Middy .NET will call the after methods of the middlewares in reverse order. You can use this to, for example, alter the response in some way (adding headers, for example).
7171

7272
### Handling errors
73-
Errors can happen in the `Before` method of the middleware or in the `After` method of them. Although we capture those errors, we treat those them slightly different.
73+
Errors can happen in the `Before` method of the middleware or in the `After` method of them. Although we capture those errors, we treat them slightly differently.
7474

7575
#### Errors on Before
7676
When an exception is thrown by a middleware in the `Before` method, the exception is captured and added to the `MiddlewareBeforeExceptions` list, so that the following middlewares and the function can react to it.
7777

7878
#### Errors on Handler
79-
When an exception is thrown by the function in its `Handle` method and before the `After` middlewares are called, the exception is captured in the `HandlerException` property of the context, so that the following middlewares can react to it.
79+
When an exception is thrown by the function in its `Handle` method and before the `After` middlewares are called, the exception is captured in the `HandlerException` property of the context, so that the following middlewares' `After` method can react to it.
8080

8181
#### Errors on After
8282
When an exception is thrown by a middleware in the `After` method, the exception is captured and added to the `MiddlewareAfterExceptions` list, so that the following middlewares can react to it.

docs/source/intro/howitworks.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ How it works
33

44
**MiddyNet** is a lightweight middleware engine. A middleware is a piece of code that can be run before or after your function is run. In the *before* section, middlewares are run in the same order that are added. In the *after* section, middlewares are run in the inverse order that are added. Usually, a middleware will only make sense to do anything in one of the sections.
55

6-
Exceptions can be thrown by the middlewares and they are captured by the library. If the exception is thrown in the *before* section, the exception is made available to the following middlewares and eventually to your function via the context. If the exception is thrown in the *after* section, it's eventually thrown by the library as an *AggregateException*.
6+
Exceptions can be thrown by the middlewares and they are captured by the library. If the exception is thrown in the *before* section, the exception is made available to the following middlewares and eventually to your function via the context. If the exception is thrown in the *handler* or in the *after* section, it's eventually thrown by the library as an *AggregateException*. Non cleaned ``MiddlewareBeforeExceptions`` will also be included in the resulting AggregateException.
77

88
**MiddyNet** also adds a Logger to log structured logging and a way to capture and propagate the TraceContext as described `here <https://www.w3.org/TR/trace-context/>`_.

docs/source/nuget/packages.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,54 @@ And like this for Http API::
263263
return Task.FromResult(result);
264264
}
265265
}
266+
267+
Voxel.MiddyNet.ProblemDetailsMiddleware
268+
---------------------------------------
269+
The middleware contained in this package formats Api exceptions as ProblemDetails following [RFC7807](https://tools.ietf.org/html/rfc7807).
270+
271+
There are two versions avalaible:
272+
273+
* One for REST Api (APIGatewayProxyRequest and APIGatewayProxyResponse).
274+
* And another for Http Api (APIGatewayHttpApiV2ProxyRequest and APIGatewayHttpApiV2ProxyResponse).
275+
276+
Configuration
277+
^^^^^^^^^^^^^
278+
It can receive a ``ProblemDetailsMiddlewareOptions`` to specify mappings from a particular exception type to an Http status code. E.g:
279+
280+
var options = new ProblemDetailsMiddlewareOptions();
281+
options.Map<NotImplementedException>(501)));
282+
283+
When a ``NotImplementedException`` is thrown, ProblemDetailsMiddleware will return the exception message with a 501 Http status code.
284+
285+
Sample code
286+
^^^^^^^^^^^
287+
A typical usage of the ProblemDetailsMiddleware for REST Api whould look something like:
288+
289+
public class ApiGatewayProblemDetails : MiddyNet<APIGatewayProxyRequest, APIGatewayProxyResponse>
290+
{
291+
public ApiGatewayProblemDetails()
292+
{
293+
Use(new ProblemDetailsMiddleware.ProblemDetailsMiddleware(new ProblemDetailsMiddlewareOptions().Map<NotImplementedException>(501)));
294+
}
295+
296+
protected override Task<APIGatewayProxyResponse> Handle(APIGatewayProxyRequest lambdaEvent, MiddyNetContext context)
297+
{
298+
throw new NotImplementedException("this will be used in the problem details description");
299+
}
300+
}
301+
302+
And for Http Api:
303+
304+
public class ApiGatewayProblemDetails : MiddyNet<APIGatewayHttpApiV2ProxyRequest, APIGatewayHttpApiV2ProxyResponse>
305+
{
306+
public ApiGatewayProblemDetails()
307+
{
308+
Use(new ProblemDetailsMiddleware.ProblemDetailsMiddleware(new ProblemDetailsMiddlewareOptions().Map<NotImplementedException>(501)));
309+
}
310+
311+
protected override Task<APIGatewayHttpApiV2ProxyResponse> Handle(APIGatewayHttpApiV2ProxyRequest lambdaEvent, MiddyNetContext context)
312+
{
313+
throw new NotImplementedException("this will be used in the problem details description");
314+
}
315+
}
316+

docs/source/started/middycontext.rst

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,25 @@ A ``Dictionary<string, object>`` filled with the information that the different
1515

1616
MiddlewareExceptions
1717
--------------------
18+
There are 4 properties and a method related to exceptions:
1819

19-
A ``List<Exception>``, with the exceptions thrown by the different middlewares.
20+
* ``List<Exception> MiddlewareBeforeExceptions { get; }``, *Middleare with the exceptions thrown by the middlewares' *Before* method.
21+
* ``Exception HandlerException { get; set; }``, with the exception thrown by the custom handler's *Handle* method.
22+
* ``List<Exception> MiddlewareAfterExceptions { get; }``, with the exceptions thrown by the middlewares' *After* method.
23+
* ``bool HasExceptions { get; }``, which tells if there are any exceptions in the aforementioned properties
24+
* ``List<Exception> GetAllExceptions()``, which returns the exceptions in the three properties as a single list, in the same order as they happened.
25+
26+
You may want to check whether any exceptions occured before proceeding with your custom logic in the handler like so:
27+
28+
if (context.HasExceptions)
29+
{
30+
// do stuff...
31+
context.MiddlewareBeforeExceptions.Clear();
32+
}
33+
34+
This will prevent those exceptions to be returned at the end of the pipeline processing.
2035

2136
Logger
2237
------
23-
An object of type MiddyLogger to allow you to log structured logs. It uses the ``ILambdaLogger`` provided by AWS internally. See more information about the :doc:`/logger`.
38+
An object of type MiddyLogger to allow you to log structured logs. It uses the ``ILambdaLogger`` provided by AWS internally. See more information about the :doc:`/logger`.
39+

docs/source/writing/writing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ The value of the property can be whatever you want, from a single type to a comp
3434

3535
Exceptions
3636
----------
37-
You don't need to catch exceptions in the middleware. The library will catch them for you and add them to the ``MiddlewareExceptions`` property.
37+
You don't need to catch exceptions in the middleware. The library will catch them for you and add them to the context's corresponding exceptions collection (``MiddlewareBeforeExceptions``, ``HandlerException`` or ``MiddlewareAfterExceptions`` depending on where was the exception thrown).

0 commit comments

Comments
 (0)