Skip to content

Commit

Permalink
moved error handling progress to new branch to keep tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
alanrjes committed Apr 22, 2023
1 parent ec9733f commit ae9987d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 55 deletions.
114 changes: 73 additions & 41 deletions src/externalApis/remoteTextApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
const axios = require('axios');
const schemas = require('./remoteTextApiValidator')

function redirectError(errorCode="") {
window.open(window.location.origin+"/error?err="+errorCode,"_self")
}

module.exports = class RemoteTextApi {

constructor() {
Expand All @@ -21,17 +25,20 @@ module.exports = class RemoteTextApi {
var data = response.data

// make sure data follows expected format and return
this.validate(data, this.schemas.listFilesOutput)
try {
this.validate(data, this.schemas.listFilesOutput)
} catch (error) {
console.log('listFiles output Schema Error')
throw error
}
return data;
})
.catch(error => {
if (error.response) {
//get HTTP error code
console.log(error.response.status)
redirectError(error.response.status)
} else {
// should we have some more sophisticated error logs?
console.log('listFiles Schema Error')
console.log(error)
redirectError("503") // this occurs when the server response is undefined, ie server can't respond.
}
})
}
Expand All @@ -41,6 +48,7 @@ module.exports = class RemoteTextApi {
try {
this.validate(newfileObject, this.schemas.createFileInput)
} catch (error) {
console.log('createFile input Schema Error')
throw error
}

Expand All @@ -50,41 +58,51 @@ module.exports = class RemoteTextApi {
var data = response.data

// make sure data follows expected format and return
this.validate(data, this.schemas.fileSummarySchema)
try {
this.validate(data, this.schemas.fileSummarySchema)
} catch (error) {
console.log('createFile output Schema Error')
throw error
}
return data;
})
.catch(error => {
if (error.response) {
//get HTTP error code
console.log(error.response.status)
redirectError(error.response.status)
} else {
// should we have some more sophisticated error logs?
console.log('createFile Schema Error')
console.log(error)
redirectError("503")
}
})
}

async getFile(fileid, githash='HEAD') {
async getFile(fileid, githash) {
if (!fileid || !githash) {redirectError("404")}

const fileObject = {id: fileid, hash: githash}
try {
this.validate(fileObject, this.schemas.getFileInput)
} catch (error) {
console.log('getFile input Schema Error')
throw error
}

return axios.put(this.url + '/getFile', {id: fileid, hash: githash})
.then(response => {
var data = response.data
this.validate(data, this.schemas.fileSummarySchema)
try {
this.validate(data, this.schemas.fileSummarySchema)
} catch (error) {
console.log('getFile output Schema Error')
throw error
}
return data;
})
.catch(error => {
if (error.response) {
console.log(error.response.status)
redirectError(error.response.status)
} else {
console.log('getFile Schema Error')
console.log(error)
redirectError("503")
}
})
}
Expand All @@ -93,24 +111,26 @@ module.exports = class RemoteTextApi {
try {
this.validate(file, this.schemas.saveFileInput)
} catch (error) {
console.log('saveFile input Schema Error')
throw error
}

return axios.put(this.url + '/saveFile', file)
.then(response => {
var data = response.data
this.validate(data, this.schemas.saveFileOutput) //Think this isn't quite right? Gonna double check
try {
this.validate(data, this.schemas.saveFileOutput)
} catch (error) {
console.log('saveFile output Schema Error')
throw error
}
return data;
})
.catch(error => {
if (error.response) {

//get HTTP error code
console.log(error.response.status)
redirectError(error.response.status)
} else {
// should we have some more sophisticated error logs?
console.log('saveFile Schema Error')
console.log(error)
redirectError("503")
}
})
}
Expand All @@ -123,75 +143,87 @@ module.exports = class RemoteTextApi {
try {
this.validate(filenameObject, this.schemas.previewFileInput)
} catch (error) {
console.log('previewFile input Schema Error')
throw error
}

return axios.put(this.url + '/previewFile', filenameObject)
.then(response => {
var data = response.data

this.validate(data, this.schemas.previewFileOutput) //Think this isn't quite right? Gonna double check
try {
this.validate(data, this.schemas.previewFileOutput)
} catch (error) {
console.log('previewFile output Schema Error')
throw error
}
return data;
})
.catch(error => {
if (error.response) {
//get HTTP error code
console.log(error.response.status)
redirectError(error.response.status)
} else {
// should we have some more sophisticated error logs?
console.log('previewFile Schema Error')
console.log(error)
redirectError("503")
}
})
}

async getPreview(fileid, githash) {
if (!fileid || !githash) {redirectError("404")}

const filenameObject = {
id: fileid,
hash: githash
}
try {
this.validate(filenameObject, this.schemas.getPreviewInput)
} catch (error) {
console.log('getPreview input Schema Error')
throw error
}

return axios.put(this.url + '/getPreview', filenameObject, { responseType: 'arraybuffer' })
.then(response => {
// this doesn't return a json so no need to validate
return response.data
})
.catch(error => {
if (error.response) {
//get HTTP error code
console.log(error.response.status)
redirectError(error.response.status)
} else {
var error_throw = " __ \n / _) \n _.----._/ / \n / error / \n __/ ( | ( | \n /__.-'|_|--|_|"
// should we have some more sophisticated error logs?
console.log(error_throw)
console.log(error)
redirectError("503")
}
})
}

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

const fileidObject={id: fileID}
try {
this.validate(fileidObject, this.schemas.getHistoryInput)
} catch (error) {
console.log('getHistory input Schema Error')
throw error
}
return axios.put(this.url + '/getHistory', fileidObject)
.then(response => {
var data = response.data
this.validate(data, this.schemas.getHistoryOutput)
try {
this.validate(data, this.schemas.getHistoryOutput)
} catch (error) {
console.log('getHistory output Schema Error')
throw error
}
return data;
})
.catch(error => {
if (error.response) {
console.log(error.response.status)
redirectError(error.response.status)
} else {
console.log('Schema Error')
console.log(error)
redirectError("503")
}
})
}
Expand All @@ -201,21 +233,21 @@ module.exports = class RemoteTextApi {
try {
this.validate(fileidObject, this.schemas.deleteFileInput)
} catch (error) {
console.log('deleteFile input Schema Error')
throw error
}

return axios.put(this.url + '/deleteFile', {id: fileid})
.then(response => {
var data = response.data
// this.validate(data, ???) do we need to validate that it doesn't return anything? seems unimportant, and was throwing error with check against null or empty string, so I'm just excluding this check.
// unsure why we're even returning anything here
return data
})
.catch(error => {
if (error.response) {
console.log(error.response.status)
redirectError(error.response.status)
} else {
console.log('deleteFile Schema Error')
console.log(error)
redirectError("503")
}
})
}
Expand Down
39 changes: 37 additions & 2 deletions src/pages/error.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
import Head from 'next/head'
import Link from 'next/link'
import styles from '../styles/Home.module.css'
import React, { useEffect, useState } from "react"

// window.open(window.location.origin+"/error?err="+errorCode, "_self")

function goHome() {
window.open(window.location.origin,"_self")
}

// for dealing with parameters
async function getQueryString() {
let queryString = window.location.search
let queryStringPromise = new Promise((resolve) => {
if (queryString != undefined) {
resolve(queryString)
}
})
return queryStringPromise
}

export default function Error() {
return (
const [errorCode, setErrorCode] = useState({})
useEffect(() => {
getQueryString()
.then(data =>{
const urlParams = new URLSearchParams(data)
setErrorCode(urlParams.get('err'))
})
})

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!" }

return ( <>
<Head>
<title>Error - RemoteText</title>
</Head>
<div id="404div">
<pre>
{ ` __
Expand All @@ -17,8 +51,9 @@ export default function Error() {
__/ ( | ( |
/__.-'|_|--|_| `}
</pre>
<p>Something went wrong!</p>
<p>{message}</p>
<button onClick={goHome}>Return to home.</button>
</div>
</>
)
}
17 changes: 5 additions & 12 deletions src/pages/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,12 @@ export default function HistoryPage() {
)
}, [])
// const foreignObjectProps = {width: nodeSize.x, height: nodeSize.y, x: 20};


if (historyTree != null) {

return (<>

return (<>
<Head>
<title>{fileName} - History</title>
</Head>
<div>
<main>
<div className={styles.imageHeader}>
<img src="/logo.png" alt="my_Logo"></img>
</div>
Expand All @@ -194,10 +191,6 @@ export default function HistoryPage() {
// }
leafNodeClassName={styles.node__leaf} />
</div>
</div>
</>)

} else {
window.open(window.location.origin + "/error", "_self")
}
</main>
</> )
}

0 comments on commit ae9987d

Please sign in to comment.