Skip to content

Commit 05fb55b

Browse files
authored
handle invalid definition id's correctly (#286)
1 parent 155d241 commit 05fb55b

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

internal/api/handlers/device_definition_handler.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handlers
22

33
import (
4+
"github.com/DIMO-Network/device-definitions-api/internal/api/common"
45
"strconv"
56
"strings"
67

@@ -62,31 +63,34 @@ type DecodeVINResponse struct {
6263
}
6364

6465
// GetDeviceDefinitionByID godoc
65-
// @Summary gets a device definition, optionally from tableland (on-chain records) if use an mmy style id.
66+
// @Summary gets a device definition, from tableland on-chain records. Only support mmy style id's eg. ford_escape_2025
6667
// @ID GetDeviceDefinitionByID
6768
// @Description gets a device definition
6869
// @Tags device-definitions
69-
// @Param id path string true "device definition id or mmy definition_id eg. ford_escape_2020"
70+
// @Param id path string true "mmy definition_id eg. ford_escape_2020"
7071
// @Produce json
7172
// @Success 200 {object} models.DeviceDefinitionTablelandModel
7273
// @Failure 404
74+
// @Failure 400
7375
// @Failure 500
7476
// @Router /device-definitions/{id} [get]
7577
func GetDeviceDefinitionByID(m mediator.Mediator) fiber.Handler {
7678
return func(c *fiber.Ctx) error {
7779
id := c.Params("id")
78-
// check if not ksuid, eg. MMY based definition_id
80+
// make sure it is mmy style dd id
7981
split := strings.Split(id, "_")
8082
if len(split) == 3 {
8183
query := &queries.GetDeviceDefinitionByIDQueryV2{DefinitionID: id}
8284
result, _ := m.Send(c.UserContext(), query)
8385
return c.Status(fiber.StatusOK).JSON(result)
8486
}
8587

86-
query := &queries.GetDeviceDefinitionByIDQuery{DeviceDefinitionID: id}
87-
result, _ := m.Send(c.UserContext(), query)
88-
89-
return c.Status(fiber.StatusOK).JSON(result)
88+
return c.Status(fiber.StatusBadRequest).JSON(common.ProblemDetails{
89+
Type: "https://tools.ietf.org/html/rfc7231#section-6.5.1",
90+
Title: "invalid id format",
91+
Status: fiber.StatusBadRequest,
92+
Detail: "id must be mmy style eg. ford_escape_2025",
93+
})
9094
}
9195
}
9296

0 commit comments

Comments
 (0)