Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 5866c34

Browse files
author
Noah Hanjun Lee
authored
feat: update to handle 403 error for the Lock tab (#123)
* Update to handle 403 error in the Lock tab * Fix the warning message
1 parent ddbe517 commit 5866c34

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

internal/server/api/v1/repos/middleware.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ func (rm *RepoMiddleware) RepoWritePerm() gin.HandlerFunc {
9999
}
100100

101101
if !(p.RepoPerm == perm.RepoPermWrite || p.RepoPerm == perm.RepoPermAdmin) {
102-
rm.log.Warn("It is denied to access the repository.", zap.String("repo_id", id), zap.Error(err))
103-
gb.AbortWithErrorResponse(c, http.StatusForbidden, "It is denied to access the repository, only write permission can access.")
102+
rm.log.Warn("The access is forbidden. Only write permission can access.", zap.String("repo_id", id))
103+
gb.AbortWithErrorResponse(c, http.StatusForbidden, "Only write permission can access.")
104104
return
105105
}
106106

@@ -142,8 +142,8 @@ func (rm *RepoMiddleware) RepoAdminPerm() gin.HandlerFunc {
142142
}
143143

144144
if p.RepoPerm != perm.RepoPermAdmin {
145-
rm.log.Warn("It is denied to access the repository.", zap.String("repo_id", id), zap.Error(err))
146-
gb.AbortWithErrorResponse(c, http.StatusForbidden, "It is denied to access the repository, only admin permission can access.")
145+
rm.log.Warn("The access is forbidden. Only admin permission can access.", zap.String("repo_id", id))
146+
gb.AbortWithErrorResponse(c, http.StatusForbidden, "Only admin permission can access.")
147147
return
148148
}
149149

ui/src/apis/lock.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { StatusCodes } from "http-status-codes"
33
import { instance, headers } from "./setting"
44
import { _fetch } from "./_base"
55
import { UserData, mapDataToUser } from "./user"
6-
import { Repo, Lock, User, HttpUnprocessableEntityError } from "../models"
6+
import { Repo, Lock, User, HttpForbiddenError, HttpUnprocessableEntityError } from "../models"
77

88
interface LockData {
99
id: number
@@ -48,7 +48,10 @@ export const lock = async (repo: Repo, env: string): Promise<Lock> => {
4848
body: JSON.stringify({env})
4949
})
5050

51-
if (res.status === StatusCodes.UNPROCESSABLE_ENTITY) {
51+
if (res.status === StatusCodes.FORBIDDEN) {
52+
const {message} = await res.json()
53+
throw new HttpForbiddenError(message)
54+
} else if (res.status === StatusCodes.UNPROCESSABLE_ENTITY) {
5255
const {message} = await res.json()
5356
throw new HttpUnprocessableEntityError(message)
5457
}
@@ -60,9 +63,14 @@ export const lock = async (repo: Repo, env: string): Promise<Lock> => {
6063
}
6164

6265
export const unlock = async (repo: Repo, lock: Lock): Promise<void> => {
63-
await _fetch(`${instance}/api/v1/repos/${repo.id}/locks/${lock.id}`, {
66+
const res = await _fetch(`${instance}/api/v1/repos/${repo.id}/locks/${lock.id}`, {
6467
headers,
6568
credentials: 'same-origin',
6669
method: "DELETE",
6770
})
71+
72+
if (res.status === StatusCodes.FORBIDDEN) {
73+
const {message} = await res.json()
74+
throw new HttpForbiddenError(message)
75+
}
6876
}

ui/src/redux/repoLock.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { createSlice, PayloadAction, createAsyncThunk } from '@reduxjs/toolkit'
2+
import { message } from "antd"
23

34
import {
45
Repo,
56
Config,
6-
Lock
7+
Lock,
8+
HttpForbiddenError
79
} from "../models"
810
import {
911
searchRepo,
@@ -79,6 +81,9 @@ export const lock = createAsyncThunk<Lock, string, { state: {repoLock: RepoLockS
7981
const locks = await _lock(repo, env)
8082
return locks
8183
} catch (e) {
84+
if (e instanceof HttpForbiddenError) {
85+
message.error("Only write permission can lock.", 3)
86+
}
8287
return rejectWithValue(e)
8388
}
8489
},
@@ -101,6 +106,9 @@ export const unlock = createAsyncThunk<Lock, string, { state: {repoLock: RepoLoc
101106
await _unlock(repo, lock)
102107
return lock
103108
} catch (e) {
109+
if (e instanceof HttpForbiddenError) {
110+
message.error("Only write permission can unlock.", 3)
111+
}
104112
return rejectWithValue(e)
105113
}
106114
},

0 commit comments

Comments
 (0)