Skip to content

Commit

Permalink
fixed error messages to match cause of error
Browse files Browse the repository at this point in the history
  • Loading branch information
alanrjes committed Apr 23, 2023
1 parent ae9987d commit 5d675b9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"next": "13.2.3",
"react": "18.2.0",
"react-d3-tree": "^3.5.1",
"react-dom": "18.2.0"
"react-dom": "18.2.0",
"validator": "^13.9.0"
}
}
52 changes: 39 additions & 13 deletions src/externalApis/remoteTextApi.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// this is an example of calling an external api
const axios = require('axios');
const schemas = require('./remoteTextApiValidator')
const validator = require('validator')

function redirectError(errorCode="") {
window.open(window.location.origin+"/error?err="+errorCode,"_self")
async function redirectError(errorCode) {
if (errorCode=="other") { // extra check to prevent other errors getting overwritten by this
axios.get(`${process.env.REMOTE_TEXT_API_URL}`+ '/listFiles') // is there a different (faster) way to just ping the server?
.catch(error=>{
window.location = window.location.origin+"/error?err=503","_self"
})
} else {
window.location = location.origin+"/error?err="+errorCode,"_self"
}
}

module.exports = class RemoteTextApi {
Expand Down Expand Up @@ -38,7 +46,7 @@ module.exports = class RemoteTextApi {
//get HTTP error code
redirectError(error.response.status)
} else {
redirectError("503") // this occurs when the server response is undefined, ie server can't respond.
redirectError("other") // this occurs when the server response is undefined, ie server can't respond, but also occurs in other API calls as a duplicate after processing a 404 error.
}
})
}
Expand Down Expand Up @@ -71,13 +79,15 @@ module.exports = class RemoteTextApi {
//get HTTP error code
redirectError(error.response.status)
} else {
redirectError("503")
redirectError("other")
}
})
}

async getFile(fileid, githash) {
if (!fileid || !githash) {redirectError("404")}
if (!validator.isUUID(fileid)) {
return redirectError("404")
}

const fileObject = {id: fileid, hash: githash}
try {
Expand All @@ -102,12 +112,16 @@ module.exports = class RemoteTextApi {
if (error.response) {
redirectError(error.response.status)
} else {
redirectError("503")
redirectError("other")
}
})
}

async saveFile(file) {
if (!validator.isUUID(file.id)) {
return redirectError("404")
}

try {
this.validate(file, this.schemas.saveFileInput)
} catch (error) {
Expand All @@ -130,12 +144,16 @@ module.exports = class RemoteTextApi {
if (error.response) {
redirectError(error.response.status)
} else {
redirectError("503")
redirectError("other")
}
})
}

async previewFile(fileid, githash) {
if (!validator.isUUID(fileid)) {
return redirectError("404")
}

const filenameObject = {
id: fileid,
hash: githash
Expand Down Expand Up @@ -164,13 +182,15 @@ module.exports = class RemoteTextApi {
//get HTTP error code
redirectError(error.response.status)
} else {
redirectError("503")
redirectError("other")
}
})
}

async getPreview(fileid, githash) {
if (!fileid || !githash) {redirectError("404")}
if (!validator.isUUID(fileid)) {
return redirectError("404")
}

const filenameObject = {
id: fileid,
Expand All @@ -193,13 +213,15 @@ module.exports = class RemoteTextApi {
//get HTTP error code
redirectError(error.response.status)
} else {
redirectError("503")
redirectError("other")
}
})
}

async getHistory(fileID) { // Parameters: File ID. Returns: A list of GitCommit objects, and a list of GitRef objects
if (!fileID) {redirectError("404")}
if (!validator.isUUID(fileID)) {
return redirectError("404")
}

const fileidObject={id: fileID}
try {
Expand All @@ -223,12 +245,16 @@ module.exports = class RemoteTextApi {
if (error.response) {
redirectError(error.response.status)
} else {
redirectError("503")
redirectError("other")
}
})
}

async deleteFile(fileid) {
if (!validator.isUUID(fileid)) {
return redirectError("404")
}

const fileidObject = {id: fileid}
try {
this.validate(fileidObject, this.schemas.deleteFileInput)
Expand All @@ -247,7 +273,7 @@ module.exports = class RemoteTextApi {
if (error.response) {
redirectError(error.response.status)
} else {
redirectError("503")
redirectError("other")
}
})
}
Expand Down
10 changes: 5 additions & 5 deletions src/pages/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export default function Error() {

var message = ""

if (errorCode == "404" ) { message = "Oops, we couldn't find that file." }
else if (errorCode == "500") { message = "An internal server error occurred."}
else if (errorCode == "503") { message = "The RemoteText server is down, please try again later."}
else if (errorCode == "???" ) { message = "... ....... ???" }
else { message = "Something went wrong!" }
if (errorCode == "404" ) { message = "(404) Oops, we couldn't find that file." }
else if (errorCode == "500") { message = "(500) An internal server error occurred." }
else if (errorCode == "503") { message = "(503) The RemoteText server is down, please try again later." }
else if (errorCode == "400") { message = "(400) Bad request: we couldn't find a version of this file with that hash." }
else { message = "("+errorCode+") Oops, something went wrong." }

return ( <>
<Head>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default function Files() {

let fileTable = <></>

if (fileData.length > 0) {
if (fileData && fileData.length > 0) {
// iterate through files to reformat timestamps (couldn't do this while in map for some reason)
fileData.forEach(file => {
file.created_time = formatTimestamp(file.created_time)
Expand Down

0 comments on commit 5d675b9

Please sign in to comment.