Skip to content
Myr

fix: Correct typos, improve documentation clarity, and remove dead code (#4511)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix: correct typos and improve documentation clarity - Fix typo "Oupps" to "Oops" in recovery test panic messages - Fix confusing documentation in Bind() and ShouldBind() methods that incorrectly stated "JSON or XML as a JSON input" - Remove double period in StaticFileFS documentation comment - Remove unused ErrorTypeNu constant that had duplicate comment with ErrorTypeAny and was never used in the codebase * tech: Fix the pull request routing link

authored by Mahan Adhikari · committed by GitHub · parent d7776de · Browse files · 5 files+19 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+19 added−21 removed

  • other:+19 added−21 removed

5 files+19 added−21 removed

modified:context.go+4 added-4 removed
749 unmodified lines
750//751// "application/json" --> JSON binding752// "application/xml" --> XML binding753//754// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.755// It decodes the json payload into the struct specified as a pointer.754// It parses the request's body based on the Content-Type (e.g., JSON or XML).755// It decodes the payload into the struct specified as a pointer.756// It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.757func (c *Context) Bind(obj any) error {758 b := binding.Default(c.Request.Method, c.ContentType())759 return c.MustBindWith(obj, b)
71 unmodified lines
831//832// "application/json" --> JSON binding833// "application/xml" --> XML binding834//835// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.836// It decodes the json payload into the struct specified as a pointer.835// It parses the request's body based on the Content-Type (e.g., JSON or XML).836// It decodes the payload into the struct specified as a pointer.837// Like c.Bind() but this method does not set the response status code to 400 or abort if input is not valid.838func (c *Context) ShouldBind(obj any) error {839 b := binding.Default(c.Request.Method, c.ContentType())840 return c.ShouldBindWith(obj, b)
642 unmodified lines
modified:CONTRIBUTING.md+1 added-1 removed
modified:errors.go-2 removed
24 unmodified lines
25 // ErrorTypePublic indicates a public error.26 ErrorTypePublic ErrorType = 1 << 127 // ErrorTypeAny indicates any other error.28 ErrorTypeAny ErrorType = 1<<64 - 129 // ErrorTypeNu indicates any other error.30 ErrorTypeNu = 229)3031// Error represents a error's specification.32type Error struct {
141 unmodified lines
modified:recovery_test.go+13 added-13 removed
20 unmodified lines
21 password := "my-super-secret-password"22 router.Use(RecoveryWithWriter(buffer))23 router.GET("/recovery", func(c *Context) {24 c.AbortWithStatus(http.StatusBadRequest)25 panic("Oupps, Houston, we have a problem")25 panic("Oops, Houston, we have a problem")26 })27 // RUN28 w := PerformRequest(router, http.MethodGet, "/recovery",29 header{
21 unmodified lines
51 buffer := new(strings.Builder)52 router := New()53 router.Use(RecoveryWithWriter(buffer))54 router.GET("/recovery", func(_ *Context) {55 panic("Oupps, Houston, we have a problem")55 panic("Oops, Houston, we have a problem")56 })57 // RUN58 w := PerformRequest(router, http.MethodGet, "/recovery")59 // TEST60 assert.Equal(t, http.StatusInternalServerError, w.Code)61 assert.Contains(t, buffer.String(), "panic recovered")62 assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")62 assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")63 assert.Contains(t, buffer.String(), t.Name())64 assert.NotContains(t, buffer.String(), "GET /recovery")6566 // Debug mode prints the request
12 unmodified lines
79 router := New()80 router.Use(RecoveryWithWriter(nil))81 router.GET("/recovery", func(c *Context) {82 c.AbortWithStatus(http.StatusBadRequest)83 panic("Oupps, Houston, we have a problem")83 panic("Oops, Houston, we have a problem")84 })85 // RUN86 w := PerformRequest(router, http.MethodGet, "/recovery")87 // TEST
73 unmodified lines
161 c.AbortWithStatus(http.StatusBadRequest)162 }163 router.Use(CustomRecoveryWithWriter(buffer, handleRecovery))164 router.GET("/recovery", func(_ *Context) {165 panic("Oupps, Houston, we have a problem")165 panic("Oops, Houston, we have a problem")166 })167 // RUN168 w := PerformRequest(router, http.MethodGet, "/recovery")169 // TEST170 assert.Equal(t, http.StatusBadRequest, w.Code)171 assert.Contains(t, buffer.String(), "panic recovered")172 assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")172 assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")173 assert.Contains(t, buffer.String(), t.Name())174 assert.NotContains(t, buffer.String(), "GET /recovery")175176 // Debug mode prints the request
3 unmodified lines
180 // TEST181 assert.Equal(t, http.StatusBadRequest, w.Code)182 assert.Contains(t, buffer.String(), "GET /recovery")183184 assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())184 assert.Equal(t, strings.Repeat("Oops, Houston, we have a problem", 2), errBuffer.String())185186 SetMode(TestMode)187}188
7 unmodified lines
196 c.AbortWithStatus(http.StatusBadRequest)197 }198 router.Use(CustomRecovery(handleRecovery))199 router.GET("/recovery", func(_ *Context) {200 panic("Oupps, Houston, we have a problem")200 panic("Oops, Houston, we have a problem")201 })202 // RUN203 w := PerformRequest(router, http.MethodGet, "/recovery")204 // TEST205 assert.Equal(t, http.StatusBadRequest, w.Code)206 assert.Contains(t, buffer.String(), "panic recovered")207 assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")207 assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")208 assert.Contains(t, buffer.String(), t.Name())209 assert.NotContains(t, buffer.String(), "GET /recovery")210211 // Debug mode prints the request
3 unmodified lines
215 // TEST216 assert.Equal(t, http.StatusBadRequest, w.Code)217 assert.Contains(t, buffer.String(), "GET /recovery")218219 assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())219 assert.Equal(t, strings.Repeat("Oops, Houston, we have a problem", 2), errBuffer.String())220221 SetMode(TestMode)222}223
7 unmodified lines
231 c.AbortWithStatus(http.StatusBadRequest)232 }233 router.Use(RecoveryWithWriter(DefaultErrorWriter, handleRecovery))234 router.GET("/recovery", func(_ *Context) {235 panic("Oupps, Houston, we have a problem")235 panic("Oops, Houston, we have a problem")236 })237 // RUN238 w := PerformRequest(router, http.MethodGet, "/recovery")239 // TEST240 assert.Equal(t, http.StatusBadRequest, w.Code)241 assert.Contains(t, buffer.String(), "panic recovered")242 assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")242 assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")243 assert.Contains(t, buffer.String(), t.Name())244 assert.NotContains(t, buffer.String(), "GET /recovery")245246 // Debug mode prints the request
3 unmodified lines
250 // TEST251 assert.Equal(t, http.StatusBadRequest, w.Code)252 assert.Contains(t, buffer.String(), "GET /recovery")253254 assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())254 assert.Equal(t, strings.Repeat("Oops, Houston, we have a problem", 2), errBuffer.String())255256 SetMode(TestMode)257}258
111 unmodified lines
modified:routergroup.go+1 added-1 removed
167 unmodified lines
168 c.File(filepath)169 })170}171172// StaticFileFS works just like `StaticFile` but a custom `http.FileSystem` can be used instead..172// StaticFileFS works just like `StaticFile` but a custom `http.FileSystem` can be used instead.173// router.StaticFileFS("favicon.ico", "./resources/favicon.ico", Dir{".", false})174// Gin by default uses: gin.Dir()175func (group *RouterGroup) StaticFileFS(relativePath, filepath string, fs http.FileSystem) IRoutes {176 return group.staticFileHandler(relativePath, func(c *Context) {
83 unmodified lines