-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04c7ef7
commit 4e9e8f8
Showing
5 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http'; | ||
import { isMatching, match, P } from 'ts-pattern'; | ||
|
||
export type UpdateRefugeError = ServerError | ClientError; | ||
|
||
export enum ServerError { | ||
UNAUTHORIZED = 'UNAUTHORIZED', | ||
FORBIDDEN = 'FORBIDDEN', | ||
NOT_FOUND = 'NOT_FOUND', | ||
CONFLICT = 'CONFLICT', | ||
UNKNOWN_ERROR = 'UNKNOWN_ERROR', | ||
INCORRECT_DATA = 'INCORRECT_DATA', | ||
} | ||
|
||
export type ClientError = { | ||
type: 'INVALID_USER_DATA'; | ||
message: string; | ||
}; | ||
|
||
export namespace UpdateRefugeError { | ||
export function fromHttp(err: HttpErrorResponse): UpdateRefugeError | never { | ||
return match(err.status) | ||
.returnType<UpdateRefugeError>() | ||
.with(0, () => { | ||
throw new Error('You are offline or the server is down.'); | ||
}) | ||
.with(HttpStatusCode.Unauthorized, () => ServerError.UNAUTHORIZED) | ||
.with(HttpStatusCode.Forbidden, () => ServerError.FORBIDDEN) | ||
.with(HttpStatusCode.NotFound, () => ServerError.NOT_FOUND) | ||
.with(HttpStatusCode.Conflict, () => ServerError.CONFLICT) | ||
.with(HttpStatusCode.UnprocessableEntity, () => | ||
getErrorFromUnprocessableEntity(err), | ||
) | ||
.otherwise(() => ServerError.UNKNOWN_ERROR); | ||
} | ||
} | ||
|
||
type UnprocessableEntityRefugeDetail = { | ||
loc: [string, number]; | ||
msg: string; | ||
type: string; | ||
}; | ||
|
||
type UnprocessableEntityRefuge = { | ||
detail: UnprocessableEntityRefugeDetail[]; | ||
}; | ||
|
||
const UnprocessableEntityRefugePattern: P.Pattern<UnprocessableEntityRefuge> = | ||
{}; | ||
|
||
function getErrorFromUnprocessableEntity( | ||
err: HttpErrorResponse, | ||
): UpdateRefugeError { | ||
const errorResponse: UnprocessableEntityRefuge = err.error; | ||
if (isMatching(UnprocessableEntityRefugePattern, errorResponse)) | ||
return { type: 'INVALID_USER_DATA', message: errorResponse.detail[0].msg }; | ||
return ServerError.INCORRECT_DATA; | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/app/schemas/refuge/update/update-refuge-response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Refuge, RefugePattern } from '../refuge'; | ||
import { isMatching } from 'ts-pattern'; | ||
import { ServerError, UpdateRefugeError } from './update-refuge-error'; | ||
import { HttpErrorResponse } from '@angular/common/http'; | ||
|
||
export type UpdateRefugeResponse = | ||
| { | ||
status: 'updated'; | ||
data: Refuge; | ||
} | ||
| { | ||
status: 'error'; | ||
error: UpdateRefugeError; | ||
}; | ||
|
||
export function updateRefugeResponseFromResponse( | ||
response: any, | ||
): UpdateRefugeResponse { | ||
if (isMatching(RefugePattern, response)) | ||
return { status: 'updated', data: response }; | ||
return { | ||
status: 'error', | ||
error: ServerError.INCORRECT_DATA, | ||
}; | ||
} | ||
|
||
export function updateRefugeResponseFromError( | ||
err: HttpErrorResponse, | ||
): UpdateRefugeResponse { | ||
return { | ||
status: 'error', | ||
error: UpdateRefugeError.fromHttp(err), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters