Fix api response for error cases

when hitting issue, the response struct should be the same as
normal response as below.
```
type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
}
```
Fix #169
This commit is contained in:
zzxwill
2020-08-18 19:48:31 +08:00
parent 79d3d3c446
commit 27cd1908ef
4 changed files with 28 additions and 2 deletions
+24
View File
@@ -29,7 +29,13 @@ var envWorldMeta = types.EnvMeta{
Namespace: "env-e2e-world",
}
var notExistedEnvMeta = types.EnvMeta{
Name: "env-e2e-api-NOT-EXISTED-JUST-FOR-TEST",
Namespace: "env-e2e-api-NOT-EXISTED-JUST-FOR-TEST",
}
var _ = ginkgo.Describe("API Env", func() {
//API Env
e2e.APIEnvInitContext("post /envs/", envHelloMeta)
ginkgo.Context("get /envs/:envName", func() {
@@ -94,4 +100,22 @@ var _ = ginkgo.Describe("API Env", func() {
gomega.Expect(r.Data.(string)).To(gomega.ContainSubstring(envWorldMeta.Name + " deleted"))
})
})
// API Application
ginkgo.Context("get /envs/:envName/apps/", func() {
ginkgo.It("should report error for not existed env", func() {
envName := notExistedEnvMeta.Name
url := fmt.Sprintf("/envs/%s/apps/", envName)
resp, err := http.Get(util.URL(url))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
var r apis.Response
err = json.Unmarshal(result, &r)
gomega.Expect(http.StatusInternalServerError).To(gomega.Equal(r.Code))
expectedContent := fmt.Sprintf("env %s not exist", envName)
gomega.Expect(r.Data.(string)).To(gomega.ContainSubstring(expectedContent))
})
})
})
+1 -1
View File
@@ -24,7 +24,7 @@ func GetEnvByName(name string) (*types.EnvMeta, error) {
data, err := ioutil.ReadFile(filepath.Join(system.GetEnvDirByName(name), system.EnvConfigName))
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("%s not exist", name)
return nil, fmt.Errorf("env %s not exist", name)
}
return nil, err
}
+1
View File
@@ -40,6 +40,7 @@ func ListApps(c *gin.Context) {
envMeta, err := oam.GetEnvByName(envName)
if err != nil {
util.HandleError(c, util.StatusInternalServerError, err)
return
}
namespace := envMeta.Namespace
+2 -1
View File
@@ -78,5 +78,6 @@ func SetErrorAndAbort(c *gin.Context, code Code, msg ...interface{}) {
}
func HandleError(c *gin.Context, code Code, msg ...interface{}) {
c.JSON(code.StatusCode(), gin.H{"error": ConstructError(code, msg...).Error()})
err := ConstructError(code, msg...)
AssembleResponse(c, nil, err)
}