MCPcopy
hub / github.com/pocketbase/pocketbase / TestBindApisErrors

Function TestBindApisErrors

plugins/jsvm/binds_test.go:1134–1186  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1132}
1133
1134func TestBindApisErrors(t *testing.T) {
1135 vm := goja.New()
1136 BindApis(vm)
1137
1138 scenarios := []struct {
1139 js string
1140 expectStatus int
1141 expectMessage string
1142 expectData string
1143 }{
1144 {"new ApiError()", 0, "", "null"},
1145 {"new ApiError(100, 'test', {'test': 1})", 100, "Test.", `{"test":1}`},
1146 {"new NotFoundError()", 404, "The requested resource wasn't found.", "null"},
1147 {"new NotFoundError('test', {'test': 1})", 404, "Test.", `{"test":1}`},
1148 {"new BadRequestError()", 400, "Something went wrong while processing your request.", "null"},
1149 {"new BadRequestError('test', {'test': 1})", 400, "Test.", `{"test":1}`},
1150 {"new ForbiddenError()", 403, "You are not allowed to perform this request.", "null"},
1151 {"new ForbiddenError('test', {'test': 1})", 403, "Test.", `{"test":1}`},
1152 {"new UnauthorizedError()", 401, "Missing or invalid authentication.", "null"},
1153 {"new UnauthorizedError('test', {'test': 1})", 401, "Test.", `{"test":1}`},
1154 {"new TooManyRequestsError()", 429, "Too Many Requests.", "null"},
1155 {"new TooManyRequestsError('test', {'test': 1})", 429, "Test.", `{"test":1}`},
1156 {"new InternalServerError()", 500, "Something went wrong while processing your request.", "null"},
1157 {"new InternalServerError('test', {'test': 1})", 500, "Test.", `{"test":1}`},
1158 }
1159
1160 for _, s := range scenarios {
1161 v, err := vm.RunString(s.js)
1162 if err != nil {
1163 t.Errorf("[%s] %v", s.js, err)
1164 continue
1165 }
1166
1167 apiErr, ok := v.Export().(*router.ApiError)
1168 if !ok {
1169 t.Errorf("[%s] Expected ApiError, got %v", s.js, v)
1170 continue
1171 }
1172
1173 if apiErr.Status != s.expectStatus {
1174 t.Errorf("[%s] Expected Status %d, got %d", s.js, s.expectStatus, apiErr.Status)
1175 }
1176
1177 if apiErr.Message != s.expectMessage {
1178 t.Errorf("[%s] Expected Message %q, got %q", s.js, s.expectMessage, apiErr.Message)
1179 }
1180
1181 dataRaw, _ := json.Marshal(apiErr.RawData())
1182 if string(dataRaw) != s.expectData {
1183 t.Errorf("[%s] Expected Data %q, got %q", s.js, s.expectData, dataRaw)
1184 }
1185 }
1186}
1187
1188func TestLoadingDynamicModel(t *testing.T) {
1189 app, _ := tests.NewTestApp()

Callers

nothing calls this directly

Calls 2

BindApisFunction · 0.85
RawDataMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…