Add error generation logic.

This commit is contained in:
Jordan Bancino 2022-09-16 11:27:34 -04:00
parent 13763eb77b
commit d0fb926f2a
3 changed files with 205 additions and 3 deletions

View file

@ -519,7 +519,7 @@ Design the server architecture
<li>Route requests</li> <li>Route requests</li>
<li><s>Handle requests</s></li> <li><s>Handle requests</s></li>
<li>Data abstraction layer</li> <li>Data abstraction layer</li>
<li>Error generation</li> <li><s>Error generation</s></li>
</ul> </ul>
</li> </li>
</ul> </ul>
@ -533,9 +533,9 @@ specification.
<li> <li>
Client-Server API Client-Server API
<ul> <ul>
<li>Error responses</li> <li><s>Error responses</s></li>
<li><code>/_matrix/client/versions</code></li> <li><code>/_matrix/client/versions</code></li>
<li>CORS Headers</li> <li><s>CORS Headers</s></li>
<li>Well-known URIs</li> <li>Well-known URIs</li>
<li>Client Authentication</li> <li>Client Authentication</li>
<li>Capabilities negotiation</li> <li>Capabilities negotiation</li>

View file

@ -25,6 +25,8 @@
#include <Matrix.h> #include <Matrix.h>
#include <HttpServer.h> #include <HttpServer.h>
#include <Json.h>
#include <Util.h>
void void
MatrixHttpHandler(HttpServerContext * context, void *argp) MatrixHttpHandler(HttpServerContext * context, void *argp)
@ -86,3 +88,158 @@ finish:
LogConfigUnindent(lc); LogConfigUnindent(lc);
} }
HashMap *
MatrixCreateError(MatrixError errorArg)
{
HashMap *errorObj;
char *errcode;
char *error;
switch (errorArg)
{
case M_FORBIDDEN:
errcode = "M_FORBIDDEN";
error = "Forbidden access. Bad permissions or not authenticated.";
break;
case M_UNKNOWN_TOKEN:
errcode = "M_UNKNOWN_TOKEN";
error = "The access or refresh token specified was not recognized.";
break;
case M_MISSING_TOKEN:
errcode = "M_MISSING_TOKEN";
error = "No access token was specified for the request.";
break;
case M_BAD_JSON:
errcode = "M_BAD_JSON";
error = "Request contained valid JSON, but it was malformed in some way.";
break;
case M_NOT_JSON:
errcode = "M_NOT_JSON";
error = "Request did not contain valid JSON.";
break;
case M_NOT_FOUND:
errcode = "M_NOT_FOUND";
error = "No resource was found for this request.";
break;
case M_LIMIT_EXCEEDED:
errcode = "M_LIMIT_EXCEEDED";
error = "Too many requests have been sent in a short period of time. "
"Wait a while then try again.";
break;
case M_UNKNOWN:
errcode = "M_UNKNOWN";
error = "An unknown error has occurred.";
break;
case M_UNRECOGNIZED:
errcode = "M_UNRECOGNIZED";
error = "The server did not understand the request.";
break;
case M_UNAUTHORIZED:
errcode = "M_UNAUTHORIZED";
error = "The request was not correctly authorized.";
break;
case M_USER_DEACTIVATED:
errcode = "M_USER_DEACTIVATED";
error = "The user ID assocated with the request has been deactivated.";
break;
case M_USER_IN_USE:
errcode = "M_USER_IN_USE";
error = "The user ID specified has already been taken.";
break;
case M_INVALID_USERNAME:
errcode = "M_INVALID_USERNAME";
error = "The user ID specified is not valid.";
break;
case M_ROOM_IN_USE:
errcode = "M_ROOM_IN_USE";
error = "The room alias given is already in use.";
break;
case M_INVALID_ROOM_STATE:
errcode = "M_INVALID_ROOM_STATE";
error = "The initial room state is invalid.";
break;
case M_THREEPID_IN_USE:
errcode = "M_THREEPID_IN_USE";
error = "The given threepid cannot be used because the same threepid is already in use.";
break;
case M_THREEPID_NOT_FOUND:
errcode = "M_THREEPID_NOT_FOUND";
error = "The given threepid cannot be used because no record matching the threepid "
"was found.";
break;
case M_THREEPID_AUTH_FAILED:
errcode = "M_THREEPID_AUTH_FAILED";
error = "Authentication could not be performed on the third party identifier.";
break;
case M_THREEPID_DENIED:
errcode = "M_THREEPID_DENIED";
error = "The server does not permit this third party identifier.";
break;
case M_SERVER_NOT_TRUSTED:
errcode = "M_SERVER_NOT_TRUSTED";
error = "The request used a third party server that this server does not trust.";
break;
case M_UNSUPPORTED_ROOM_VERSION:
errcode = "M_UNSUPPORTED_ROOM_VERSION";
error = "The request to create a room used a room version that the server "
"does not support.";
break;
case M_INCOMPATIBLE_ROOM_VERSION:
errcode = "M_INCOMPATIBLE_ROOM_VERSION";
error = "Attempted to join a room that has a version the server does not support.";
break;
case M_BAD_STATE:
errcode = "M_BAD_STATE";
error = "The state change requested cannot be performed.";
break;
case M_GUEST_ACCESS_FORBIDDEN:
errcode = "M_GUEST_ACCESS_FORBIDDEN";
error = "The room or resource does not permit guests to access it.";
break;
case M_CAPTCHA_NEEDED:
errcode = "M_CAPTCHA_NEEDED";
error = "A Captcha is required to complete the request.";
break;
case M_CAPTCHA_INVALID:
errcode = "M_CAPTCHA_INVALID";
error = "The Captcha provided did not match what was expected.";
break;
case M_MISSING_PARAM:
errcode = "M_MISSING_PARAM";
error = "A required parameter was missing from the request.";
break;
case M_TOO_LARGE:
errcode = "M_TOO_LARGE";
error = "The request or entity was too large.";
break;
case M_EXCLUSIVE:
errcode = "M_EXCLUSIVE";
error = "The resource being requested is reserved by an application service, "
"or the application service making the request has not created the resource.";
break;
case M_RESOURCE_LIMIT_EXCEEDED:
errcode = "M_RESOURCE_LIMIT_EXCEEDED";
error = "The request cannot be completed because the homeserver has reached "
"a resource limit imposed on it.";
break;
case M_CANNOT_LEAVE_SERVER_NOTICE_ROOM:
errcode = "M_CANNOT_LEAVE_SERVER_NOTICE_ROOM";
error = "The user is unable to reject an invite to join the server notices room.";
break;
default:
return NULL;
}
errorObj = HashMapCreate();
if (!errorObj)
{
return NULL;
}
HashMapSet(errorObj, "errcode", JsonValueString(UtilStringDuplicate(errcode)));
HashMapSet(errorObj, "error", JsonValueString(UtilStringDuplicate(error)));
return errorObj;
}

View file

@ -26,6 +26,48 @@
#include <HttpServer.h> #include <HttpServer.h>
#include <Log.h> #include <Log.h>
#include <HashMap.h>
typedef enum MatrixError
{
M_FORBIDDEN,
M_UNKNOWN_TOKEN,
M_MISSING_TOKEN,
M_BAD_JSON,
M_NOT_JSON,
M_NOT_FOUND,
M_LIMIT_EXCEEDED,
M_UNKNOWN,
M_UNRECOGNIZED,
M_UNAUTHORIZED,
M_USER_DEACTIVATED,
M_USER_IN_USE,
M_INVALID_USERNAME,
M_ROOM_IN_USE,
M_INVALID_ROOM_STATE,
M_THREEPID_IN_USE,
M_THREEPID_NOT_FOUND,
M_THREEPID_AUTH_FAILED,
M_THREEPID_DENIED,
M_SERVER_NOT_TRUSTED,
M_UNSUPPORTED_ROOM_VERSION,
M_INCOMPATIBLE_ROOM_VERSION,
M_BAD_STATE,
M_GUEST_ACCESS_FORBIDDEN,
M_CAPTCHA_NEEDED,
M_CAPTCHA_INVALID,
M_MISSING_PARAM,
M_INVALID_PARAM,
M_TOO_LARGE,
M_EXCLUSIVE,
M_RESOURCE_LIMIT_EXCEEDED,
M_CANNOT_LEAVE_SERVER_NOTICE_ROOM
} MatrixError;
typedef struct MatrixHttpHandlerArgs typedef struct MatrixHttpHandlerArgs
{ {
@ -35,4 +77,7 @@ typedef struct MatrixHttpHandlerArgs
extern void extern void
MatrixHttpHandler(HttpServerContext *, void *); MatrixHttpHandler(HttpServerContext *, void *);
extern HashMap *
MatrixCreateError(MatrixError);
#endif #endif