/
ApiStatusCodesZ.scala
49 lines (40 loc) · 1.76 KB
/
ApiStatusCodesZ.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.http.status
import zhttp.http.Status
import dsp.errors._
import org.knora.webapi.store.triplestore.errors.TriplestoreTimeoutException
/**
* The possible values for the HTTP status code that is returned as part of each Knora API v2 response.
* Migrated from [[org.knora.webapi.http.status.ApiStatusCodes]]
*/
object ApiStatusCodesZ {
/**
* Converts an exception to a suitable HTTP status code.
*
* @param ex an exception.
* @return an HTTP status code.
*/
def fromExceptionZ(ex: Throwable): Status =
ex match {
// Subclasses of RequestRejectedException
case NotFoundException(_) => Status.NotFound
case ForbiddenException(_) => Status.Forbidden
case BadCredentialsException(_) => Status.Unauthorized
case DuplicateValueException(_) => Status.BadRequest
case OntologyConstraintException(_) => Status.BadRequest
case EditConflictException(_) => Status.Conflict
case BadRequestException(_) => Status.BadRequest
case ValidationException(_, _) => Status.BadRequest
case RequestRejectedException(_) => Status.BadRequest
// RequestRejectedException must be the last one in this group
// Subclasses of InternalServerException
case UpdateNotPerformedException(_) => Status.Conflict
case TriplestoreTimeoutException(_, _) => Status.GatewayTimeout
case InternalServerException(_) => Status.InternalServerError
// InternalServerException must be the last one in this group
case _ => Status.InternalServerError
}
}