Skip to content

Commit 3b2e43b

Browse files
authored
Fix: issue 41- empty phase name panic (#42)
* require phase name Signed-off-by: Joshua Irmer <irmer@gonicus.de> * check for existing phase before creation Signed-off-by: Joshua Irmer <irmer@gonicus.de> --------- Signed-off-by: Joshua Irmer <irmer@gonicus.de>
1 parent 81d96d0 commit 3b2e43b

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

pkg/db/phase.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import apiServerDefinition "github.com/SovereignCloudStack/status-page-openapi/p
44

55
// Phase represents a state of an incident on a moving scale to resolution of the incident.
66
type Phase struct {
7-
Name *apiServerDefinition.Phase `yaml:"name"`
7+
Name *apiServerDefinition.Phase `gorm:"not null" yaml:"name"`
88
Generation *apiServerDefinition.Incremental `gorm:"primaryKey"`
99
Order *apiServerDefinition.Incremental `gorm:"primaryKey"`
1010
}

pkg/server/incident.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (i *Implementation) GetIncidents(ctx echo.Context, params apiServerDefiniti
6868
}
6969

7070
// CreateIncident handles creation of incidents.
71-
func (i *Implementation) CreateIncident(ctx echo.Context) error {
71+
func (i *Implementation) CreateIncident(ctx echo.Context) error { //nolint: funlen
7272
var request apiServerDefinition.CreateIncidentJSONRequestBody
7373

7474
logger := i.logger.With().Str("handler", "CreateIncident").Logger()
@@ -97,11 +97,41 @@ func (i *Implementation) CreateIncident(ctx echo.Context) error {
9797

9898
dbSession := i.dbCon.WithContext(ctx.Request().Context())
9999

100-
res := dbSession.Create(incident)
101-
if res.Error != nil {
102-
logger.Error().Err(res.Error).Msg("error creating incident")
100+
err = dbSession.Transaction(func(dbTx *gorm.DB) error {
101+
var (
102+
transactionErr error
103+
dbPhase DbDef.Phase
104+
)
103105

104-
return echo.ErrInternalServerError
106+
// Check phase validity
107+
if incident.Phase != nil {
108+
dbPhase.Generation = incident.Phase.Generation
109+
dbPhase.Order = incident.Phase.Order
110+
111+
transactionErr = dbTx.First(&dbPhase).Error
112+
if errors.Is(transactionErr, gorm.ErrRecordNotFound) {
113+
logger.Warn().Msg("invalid phase for incident")
114+
115+
return echo.ErrBadRequest
116+
} else if transactionErr != nil {
117+
logger.Error().Err(transactionErr).Msg("error loading phase from database")
118+
119+
return echo.ErrInternalServerError
120+
}
121+
}
122+
123+
transactionErr = dbTx.Create(incident).Error
124+
if transactionErr != nil {
125+
logger.Error().Err(transactionErr).Msg("error creating incident")
126+
127+
return echo.ErrInternalServerError
128+
}
129+
130+
return nil
131+
})
132+
if err != nil {
133+
// Don't wrap the echo errors.
134+
return err //nolint:wrapcheck
105135
}
106136

107137
return ctx.JSON(http.StatusCreated, apiServerDefinition.IdResponse{ //nolint:wrapcheck

0 commit comments

Comments
 (0)