mirror of https://github.com/Squidex/squidex.git
13 changed files with 186 additions and 141 deletions
@ -0,0 +1,40 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { ErrorDto } from './../'; |
|||
|
|||
describe('ErrorDto', () => { |
|||
it('Should create simple message when no details are specified.', () => { |
|||
const error = new ErrorDto(500, 'Error Message.'); |
|||
|
|||
expect(error.displayMessage).toBe('Error Message.'); |
|||
}); |
|||
|
|||
it('Should append dot to message', () => { |
|||
const error = new ErrorDto(500, 'Error Message'); |
|||
|
|||
expect(error.displayMessage).toBe('Error Message.'); |
|||
}); |
|||
|
|||
it('Should create simple message when detail has one item', () => { |
|||
const error = new ErrorDto(500, 'Error Message.', ['Detail Message.']); |
|||
|
|||
expect(error.displayMessage).toBe('Error Message: Detail Message.'); |
|||
}); |
|||
|
|||
it('Should create append do to simple message when detail has one item', () => { |
|||
const error = new ErrorDto(500, 'Error Message', ['Detail Message']); |
|||
|
|||
expect(error.displayMessage).toBe('Error Message: Detail Message.'); |
|||
}); |
|||
|
|||
it('Should create html list when error has more items.', () => { |
|||
const error = new ErrorDto(500, 'Error Message', ['Detail1.', 'Detail2.']); |
|||
|
|||
expect(error.displayMessage).toBe('Error Message.<ul><li>Detail1.</li><li>Detail2.</li></ul>'); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,57 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
export class ErrorDto { |
|||
public readonly displayMessage: string; |
|||
|
|||
constructor( |
|||
public readonly statusCode: number, |
|||
public readonly message: string, |
|||
public readonly details: string[] = [] |
|||
) { |
|||
this.displayMessage = formatMessage(message, details); |
|||
} |
|||
} |
|||
|
|||
|
|||
function formatMessage(message: string, details?: string[]) { |
|||
const appendLast = (row: string, char: string) => { |
|||
const last = row[row.length - 1]; |
|||
|
|||
if (last !== char) { |
|||
return row + char; |
|||
} else { |
|||
return row; |
|||
} |
|||
}; |
|||
|
|||
const removeLast = (row: string, char: string) => { |
|||
const last = row[row.length - 1]; |
|||
|
|||
if (last === char) { |
|||
return row.substr(0, row.length - 1); |
|||
} else { |
|||
return row; |
|||
} |
|||
}; |
|||
|
|||
if (details && details.length > 1) { |
|||
let result = appendLast(message, '.') + '<ul>'; |
|||
|
|||
for (let detail of details) { |
|||
result += `<li>${appendLast(detail, '.')}</li>`; |
|||
} |
|||
|
|||
result = result + '</ul>'; |
|||
|
|||
return result; |
|||
} else if (details && details.length === 1) { |
|||
return `${appendLast(removeLast(message, '.'), ':')} ${appendLast(details[0], '.')}`; |
|||
} else { |
|||
return appendLast(message, '.'); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue