Skip to content

Commit f459099

Browse files
knieczyszczakfrantuma
authored andcommitted
SWG-13871 sending errors to BugSnag for pet endpoints
1 parent e28cf32 commit f459099

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Dockerfile-telemetry

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ ENV JAVA_TOOL_OPTIONS="-javaagent:/swagger-petstore/otel-javaagent.jar" \
2828
OTEL_INSTRUMENTATION_HTTP_SERVER_CAPTURE_ROUTE=true \
2929
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT=256
3030

31-
CMD ["sh", "-c", "export OTEL_EXPORTER_OTLP_ENDPOINT=https://$BUGSNAG_API_KEY.otlp.bugsnag.com && \
31+
CMD ["sh", "-c", "export BUGSNAG_API_KEY=$BUGSNAG_API_KEY && export OTEL_EXPORTER_OTLP_ENDPOINT=https://$BUGSNAG_API_KEY.otlp.bugsnag.com && \
3232
exec java $JAVA_TOOL_OPTIONS -Dorg.eclipse.jetty.server.RequestLog=DEBUG -Dorg.eclipse.jetty.server.HttpChannel=DEBUG \
3333
-jar -DswaggerUrl=openapi.yaml /swagger-petstore/jetty-runner.jar --log /var/log/yyyy_mm_dd-requests.log /swagger-petstore/server.war"]

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@
215215
<artifactId>jaxb-impl</artifactId>
216216
<version>2.3.1</version>
217217
</dependency>
218+
<dependency>
219+
<groupId>com.bugsnag</groupId>
220+
<version>[3.0,4.0)</version>
221+
<artifactId>bugsnag</artifactId>
222+
</dependency>
218223

219224
</dependencies>
220225
<repositories>

src/main/java/io/swagger/petstore/controller/PetController.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.swagger.petstore.controller;
1818

19+
import com.bugsnag.Bugsnag;
1920
import io.swagger.oas.inflector.models.RequestContext;
2021
import io.swagger.oas.inflector.models.ResponseContext;
2122
import io.swagger.petstore.data.PetData;
@@ -33,9 +34,20 @@
3334
public class PetController {
3435

3536
private static PetData petData = new PetData();
37+
private static Bugsnag bugsnag;
38+
39+
static {
40+
String bugsnagApiKey = System.getenv("BUGSNAG_API_KEY");
41+
if (bugsnagApiKey != null) {
42+
bugsnag = new Bugsnag(bugsnagApiKey);
43+
} else {
44+
throw new IllegalStateException("BUGSNAG_API_KEY environment variable is not set");
45+
}
46+
}
3647

3748
public ResponseContext findPetsByStatus(final RequestContext request, final String status) {
3849
if (status == null) {
50+
bugsnag.notify(new RuntimeException("No status provided"));
3951
return new ResponseContext()
4052
.status(Response.Status.BAD_REQUEST)
4153
.entity("No status provided. Try again?");
@@ -44,16 +56,19 @@ public ResponseContext findPetsByStatus(final RequestContext request, final Stri
4456
final List<Pet> petByStatus = petData.findPetByStatus(status);
4557

4658
if (petByStatus == null) {
59+
bugsnag.notify(new RuntimeException("Pets not found"));
4760
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pets not found");
4861
}
4962

63+
bugsnag.notify(new RuntimeException("Pets not found"));
5064
return new ResponseContext()
5165
.contentType(Util.getMediaType(request))
5266
.entity(petByStatus);
5367
}
5468

5569
public ResponseContext getPetById(final RequestContext request, final Long petId) {
5670
if (petId == null) {
71+
bugsnag.notify(new RuntimeException("No petId provided"));
5772
return new ResponseContext()
5873
.status(Response.Status.BAD_REQUEST)
5974
.entity("No petId provided. Try again?");
@@ -67,17 +82,20 @@ public ResponseContext getPetById(final RequestContext request, final Long petId
6782
.entity(pet);
6883
}
6984

85+
bugsnag.notify(new RuntimeException("Pets not found"));
7086
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
7187
}
7288

7389
public ResponseContext updatePetWithForm(final RequestContext request, final Long petId, final String name, final String status) {
7490
if (petId == null) {
91+
bugsnag.notify(new RuntimeException("No petId provided"));
7592
return new ResponseContext()
7693
.status(Response.Status.BAD_REQUEST)
7794
.entity("No Pet provided. Try again?");
7895
}
7996

8097
if (name == null) {
98+
bugsnag.notify(new RuntimeException("No name provided"));
8199
return new ResponseContext()
82100
.status(Response.Status.BAD_REQUEST)
83101
.entity("No Name provided. Try again?");
@@ -87,6 +105,7 @@ public ResponseContext updatePetWithForm(final RequestContext request, final Lon
87105
final Pet existingPet = petData.getPetById(petId);
88106

89107
if (existingPet == null) {
108+
bugsnag.notify(new RuntimeException("No pet provided"));
90109
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
91110
}
92111

@@ -102,6 +121,7 @@ public ResponseContext updatePetWithForm(final RequestContext request, final Lon
102121

103122
public ResponseContext deletePet(final RequestContext request, final String apiKey, final Long petId) {
104123
if (petId == null) {
124+
bugsnag.notify(new RuntimeException("No petId provided"));
105125
return new ResponseContext()
106126
.status(Response.Status.BAD_REQUEST)
107127
.entity("No petId provided. Try again?");
@@ -118,24 +138,28 @@ public ResponseContext deletePet(final RequestContext request, final String apiK
118138
.contentType(outputType)
119139
.entity("Pet deleted");
120140
} else {
141+
bugsnag.notify(new RuntimeException("Pet couldn't be deleted"));
121142
return new ResponseContext().status(Response.Status.NOT_MODIFIED).entity("Pet couldn't be deleted.");
122143
}
123144

124145
}
125146

126147
public ResponseContext uploadFile(final RequestContext request, final Long petId, final String apiKey, final File file) {
127148
if (petId == null) {
149+
bugsnag.notify(new RuntimeException("No petId provided"));
128150
return new ResponseContext()
129151
.status(Response.Status.BAD_REQUEST)
130152
.entity("No petId provided. Try again?");
131153
}
132154

133155
if (file == null) {
156+
bugsnag.notify(new RuntimeException("No file provided"));
134157
return new ResponseContext().status(Response.Status.BAD_REQUEST).entity("No file uploaded");
135158
}
136159

137160
final Pet existingPet = petData.getPetById(petId);
138161
if (existingPet == null) {
162+
bugsnag.notify(new RuntimeException("No pet provided"));
139163
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
140164
}
141165

@@ -150,12 +174,14 @@ public ResponseContext uploadFile(final RequestContext request, final Long petId
150174
.contentType(Util.getMediaType(request))
151175
.entity(pet);
152176
} else {
177+
bugsnag.notify(new RuntimeException("Pet couldn't be updated"));
153178
return new ResponseContext().status(Response.Status.NOT_MODIFIED).entity("Pet couldn't be updated.");
154179
}
155180
}
156181

157182
public ResponseContext addPet(final RequestContext request, final Pet pet) {
158183
if (pet == null) {
184+
bugsnag.notify(new RuntimeException("No pet provided"));
159185
return new ResponseContext()
160186
.status(Response.Status.BAD_REQUEST)
161187
.entity("No Pet provided. Try again?");
@@ -176,13 +202,15 @@ public ResponseContext addPet(final RequestContext request, final Long id, final
176202

177203
public ResponseContext updatePet(final RequestContext request, final Pet pet) {
178204
if (pet == null) {
205+
bugsnag.notify(new RuntimeException("No pet provided"));
179206
return new ResponseContext()
180207
.status(Response.Status.BAD_REQUEST)
181208
.entity("No Pet provided. Try again?");
182209
}
183210

184211
final Pet existingPet = petData.getPetById(pet.getId());
185212
if (existingPet == null) {
213+
bugsnag.notify(new RuntimeException("No pet provided"));
186214
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
187215
}
188216

@@ -202,6 +230,7 @@ public ResponseContext updatePet(final RequestContext request, final Long id, fi
202230

203231
public ResponseContext findPetsByTags(final RequestContext request, final List<String> tags) {
204232
if (tags == null || tags.size() == 0) {
233+
bugsnag.notify(new RuntimeException("No tags provided"));
205234
return new ResponseContext()
206235
.status(Response.Status.BAD_REQUEST)
207236
.entity("No tags provided. Try again?");

0 commit comments

Comments
 (0)